首页 > 编程语言 > 详细

java可变参数

时间:2018-01-22 18:27:28      阅读:270      评论:0      收藏:0      [点我收藏+]

 

    //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

    }

 

java可变参数

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!