public static  void test(String ... strs){
for(String n:strs){
System.out.println(n);
}
}
public static void main(String[] args) throws Exception {
test("a","b");
test("c");
}

解决方案 »

  1.   

    给你举个例子你就明白了,多说无益public static int add(int ... args){       int total = 0;    
           for (int i = 0; i < args.length; i++)
                  total += args[i];      
           return total;
    }
    public static void main(String[] args){
           int a;
           a = Varargs.add(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
           System.out.println(a);
    }
      

  2.   


    楼上的几位说得很清楚了,但这个特性在1.4中也可以实现,我将2楼的改改如下:public class Test {
    public static void test(String[] strs) {
    for (int i=0;i<strs.length; i++) {
    System.out.println(strs[i]);
    }
    } public static void main(String[] args) throws Exception {
    test(new String[]{"a", "b"});
    test(new String[]{"c"});
    }
    }
      

  3.   

    String ... strs
    只是编译器稍微处理了一下,编译之后还是按数组来处理的。