您的位置:首页 > 编程语言 > Java开发

java 传参数时 类型后跟 3个点 "..." 的意义

2018-02-05 12:40 441 查看
1、直接看代码就知道什么含义了!

public class StringDemo{

    public static void main(String[] args){  

        testPoints("I love my job.");//一个参数传入  

        testPoints("you","and","me");//3个String参数传入  

        testPoints(new String[]{"you","and","me"});//可以看到传入三个String参数和传入一个长度为3的数组结果一样,再看例子  

        System.out.println("---------------------------------------------------------");  

          

        testPoints(7);  

        testPoints(7,9,11);  

        testPoints(new Integer[]{7,9,11});  

    }  

      

    public static void testPoints(String... s){  

        if(s.length==0){

            System.out.println("没有参数传入!");  

        }else if(s.length==1){  

            System.out.println("1个参数传入!");  

        }else{      

            System.out.println("the input string is-->");  

            for(int i=0;i<s.length;i++){  

                System.out.println("第"+(i+1)+"个参数是"+s[i]+";");  

            }      

            System.out.println();  

        }  

    }  

      

    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("the input string is-->");  

            for(int i=0;i<itgr.length;i++){  

                System.out.println("第"+(i+1)+"个Integer参数是"+itgr[i]+";");  

            }      

            System.out.println();  

        }  

    }  

  

}  

--------------------------------------------------------  

输出:  

1个参数传入!  

the input string is-->  

第1个参数是you;  

第2个参数是and;  

第3个参数是me;  

  

the input string is-->  

第1个参数是you;  

第2个参数是and;  

第3个参数是me;  

  

---------------------------------------------------------  

1个Integer参数传入!  

the input string is-->  

第1个Integer参数是7;  

第2个Integer参数是9;  

第3个Integer参数是11;  

  

the input string is-->  

第1个Integer参数是7;  

第2个Integer参数是9;  

第3个Integer参数是11; 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: