总听别人说可变参数,怎么用啊,求指点?最好详细点~

解决方案 »

  1.   


    public class TestArgs {
    public static void test1(Integer ...a){
    for(Integer i :a ){
    System.out.println(i);
    }
    }
    public static void test2(Integer a, Integer ...b){
    for(Integer i :b ){
    System.out.println(i);
    }
    }
    public static void main(String[] args) {
    System.out.println("test1");
    test1(1,2,3,4,5);
    System.out.println("test2");
    test2(1,2,3,4,5);
    }
    }
    (1)、只能出现在参数列表的最后; (2)、...位于变量类型和变量名之间,前后有无空格都可以;(3)、调用可变参数的方法时,编译器为该可变参数隐含创建一个数组,在方法体中一数组的形式访问可变参数。
      

  2.   

    Java is C++ without the guns, knives, and club
      

  3.   

    method(String...arr){
    打印arr[0]arr[1]
    }其实就是把arr当数组看