- 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;
java 传参数时 类型后跟 3个点 “...” 的意义
最新推荐文章于 2022-12-12 15:04:25 发布