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

来源:互联网 发布:win10系统java环境变量 编辑:程序博客网 时间:2024/05/21 11:34
Java代码  收藏代码
  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;  
原创粉丝点击