1. 程式人生 > >java中方法中宣告三個點“...”作用

java中方法中宣告三個點“...”作用

public class Test {
 public static void main(String[] args) {
  String str[] = {"s","f"};
  
//  test();      //可以為空
//  test("www");     //一個string
//  test(str);       //一個string陣列
  test("w","d","s");    //多個string

 }
 public static void test(String ...args){
  for (int i = 0; i < args.length; i++) {
   System.out.println(args[i]);


  }
 }
}

jdk1.5之後可以使用這種宣告方式,表示可以多種方式傳參,可以為空,可以傳一個,可以傳一個數組,可以傳多個字串等