[转]java 传参数时 类型后跟 3个点 “...” 的意义

 1 public class StringDemo{  
 2     public static void main(String[] args){  
 3         testPoints("I love my job.");//一个参数传入  
 4         testPoints("you","and","me");//3个String参数传入  
 5         testPoints(new String[]{"you","and","me"});//可以看到传入三个String参数和传入一个长度为3的数组结果一样,再看例子  
 6         System.out.println("---------------------------------------------------------");  
 7           
 8         testPoints(7);  
 9         testPoints(7,9,11);  
10         testPoints(new Integer[]{7,9,11});  
11     }  
12       
13     public static void testPoints(String... s){  
14         if(s.length==0){  
15             System.out.println("没有参数传入!");  
16         }else if(s.length==1){  
17             System.out.println("1个参数传入!");  
18         }else{      
19             System.out.println("the input string is-->");  
20             for(int i=0;i<s.length;i++){  
21                 System.out.println("第"+(i+1)+"个参数是"+s[i]+";");  
22             }      
23             System.out.println();  
24         }  
25     }  
26       
27     public static void testPoints(Integer... itgr){  
28         if(itgr.length==0){  
29             System.out.println("没有Integer参数传入!");  
30         }else if(itgr.length==1){  
31             System.out.println("1个Integer参数传入!");  
32         }else{      
33             System.out.println("the input string is-->");  
34             for(int i=0;i<itgr.length;i++){  
35                 System.out.println("第"+(i+1)+"个Integer参数是"+itgr[i]+";");  
36             }      
37             System.out.println();  
38         }  
39     }  
40   
41 }  
42 --------------------------------------------------------  
43 输出:  
44 1个参数传入!  
45 the input string is-->  
46 第1个参数是you;  
47 第2个参数是and;  
48 第3个参数是me;  
49   
50 the input string is-->  
51 第1个参数是you;  
52 第2个参数是and;  
53 第3个参数是me;  
54   
55 ---------------------------------------------------------  
56 1个Integer参数传入!  
57 the input string is-->  
58 第1个Integer参数是7;  
59 第2个Integer参数是9;  
60 第3个Integer参数是11;  
61   
62 the input string is-->  
63 第1个Integer参数是7;  
64 第2个Integer参数是9;  
65 第3个Integer参数是11;  

原文地址:http://blog.csdn.net/gst6062825/article/details/7614433

posted on 2018-03-17 02:49  805772248  阅读(340)  评论(0)    收藏  举报

导航