java可变参数

    //int(type) followed ... (three dot's) is syntax of a variable argument.
    public static int sum(int... numbers) {
        //inside the method a variable argument is similar to an array.
        // number can be treated as if it is declared as int[] numbers;
        int sum = 0;
        for (int number : numbers) {
            sum += number;
        }
        return sum;
    }

    public static void main(String[] args) {
        // 3 Arguments
        System.out.println(sum(1, 4, 5)); // 10

        // 4 Arguments
        System.out.println(sum(1, 4, 5, 20));//30
        // 0 Arguments
        System.out.println(sum()); // 0

    }
原文地址:https://www.cnblogs.com/chenglc/p/8330054.html