Java中“...”的使用

    该用法的意思是--可变长参数,就是说这个位置可以传入任意个该类型参数,简单来说就是个数组。

    示例:

    

package com.yzl.attachedtest;

public class PointsTest {

public static void testPoints(Integer... itgr){
if(itgr.length == 0){
System.out.println("没有integer参数传入");
}else if(itgr.length == 1){
System.out.println("1个integer参数传入");
}else{
System.out.println("传入的参数是:");
for(int i = 0;i < itgr.length;i++){
System.out.println("第" + (i+1) + "个参数是:" + itgr[i] + ";");
}
}
}

public static void main(String[] args) {
testPoints();
testPoints(7);
testPoints(7,9);
testPoints(new Integer[]{7,9,11});

}

}

输出的结果是:

没有integer参数传入
1个integer参数传入
传入的参数是:
第1个参数是:7;
第2个参数是:9;
传入的参数是:
第1个参数是:7;
第2个参数是:9;
第3个参数是:11;

posted @ 2018-12-16 15:48  abortre  阅读(461)  评论(0编辑  收藏  举报