Java中有不定参数的用法。
用法如下例:
--方法定义
public void set(String... strings) {
for(String s : strings) {
System.out.println(s);
}
}
--调用
new Test().set("hi1", "hi2");
new Test().set("hi1");
new Test().set();
对于不定参数,编译器编译时会将其转换成数组调用的方式。如下例:
--方法定义
public void set(String[] strings) {
for(String s : strings) {
System.out.println(s);
}
}
--调用
new Test().set(new String[]{"hi1", "hi2"});
new Test().set(new String[]{"hi1"});
new Test().set(new String[]{});