随笔-126  评论-247  文章-5  trackbacks-0

可变参数的特点:

    1. 在变量类型之后用 "..." 来声明该变量为可变的,"..." 位于变量类型和变量名之间,中间有无空格都行

    2. 只能出现在参数列表的最后,作为最后一个参数传递到方法中

    3. 可变参数相当于一个长度不确定的数组,在方法体中可以以数组的形式来访问可变参数

For a simple example:


public class TestApp {

    
public static void main(String[] args){
    
        System.out.println(add(
1,2,3));
        System.out.println(add(
1,2,3,4,5));
    }
    
    
private static int add(int ... args){
        
int sum = 0;
        
for(int index : args){
            sum 
+= index;
        }
        
return sum;
    }
    
}



  
posted on 2012-08-24 11:02 fancydeepin 阅读(755) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: