zoukankan      html  css  js  c++  java
  • java 传参数时 类型后跟 3个点 "..." 的意义

    对照代码和运行结果便知"..." 的意义

     1 import java.util.ArrayList;
     2 
     3 public class StringDemo {
     4     public static void main(String[] args) {
     5         testPoint("LOL"); //一个参数传入
     6         testPoint("L","O","L"); //3个String参数传入
     7         testPoint(new String[] {"L","O","L"}); //可以看到传入三个String参数和传入一个长度为3的数组结果一样
     8         
     9         testPoint(6);
    10         testPoint(6,6,6);
    11         testPoint(new Integer[] {6,6,6});
    12         
    13     }
    14     
    15     public static void testPoint(String ...s) {
    16         if(s.length == 0) {
    17             System.out.println("没有参数传入!");
    18         }else if(s.length == 1) {
    19             System.out.println("有一个参数传入,它是: "+s[0]);
    20             System.out.println("------------------------");
    21         }else {
    22             System.out.println("the input String is-->");
    23             for(int i = 0;i < s.length;++i) {
    24                 System.out.println("第" + (i+1) +"个参数是"+s[i]+";");
    25             }
    26         }
    27     }
    28     
    29     public static void testPoint(Integer ...itgr) {
    30         if(itgr.length == 0) {
    31             System.out.println("没有整数传入!");
    32         }else if(itgr.length == 1) {
    33             System.out.println("有一个整数传入,它是: "+itgr[0]);
    34             System.out.println("------------------------");
    35         }else {
    36             System.out.println("the input String is-->");
    37             for(int i = 0;i < itgr.length;++i) {
    38                 System.out.println("第" + (i+1) +"个整数是"+itgr[i]+";");
    39             }
    40         }
    41     }
    42 
    43 }

    运行结果如下:

    有一个参数传入,它是: LOL
    ------------------------
    the input String is-->
    第1个参数是L;
    第2个参数是O;
    第3个参数是L;
    the input String is-->
    第1个参数是L;
    第2个参数是O;
    第3个参数是L;
    有一个整数传入,它是: 6
    ------------------------
    the input String is-->
    第1个整数是6;
    第2个整数是6;
    第3个整数是6;
    the input String is-->
    第1个整数是6;
    第2个整数是6;
    第3个整数是6;

  • 相关阅读:
    HDOJ 4747 Mex
    HDU 1203 I NEED A OFFER!
    HDU 2616 Kill the monster
    HDU 3496 Watch The Movie
    Codeforces 347A A. Difference Row
    Codeforces 347B B. Fixed Points
    Codeforces 372B B. Hungry Sequence
    HDU 1476 Sudoku Killer
    HDU 1987 How many ways
    HDU 2564 词组缩写
  • 原文地址:https://www.cnblogs.com/pangxiaoshuai/p/10795022.html
Copyright © 2011-2022 走看看