今天看别人的代码,发现有人在参数列表中使用省略号“...”,不知何故,故Google之。
英文名字叫 Variable-length arguments list, 具体不知道该怎么翻译(英文太烂。。。)所以直译为
可变参数列表。一下是可变参数列表的一个例子。
public class VariArg {
public static void main(String[] args) {
VariArg va = new VariArg();
double d1 = 2.3;
double d2 = 2.5;
double d3 = 9;
System.out.println("variable length argument list demo");
System.out.println("the average of " + d1 + " " + d2 + " is "
+ va.mean(d1, d2));
System.out.println("the average of " + d1 + " " + d2 + " " + d3
+ " is " + va.mean(d1, d2, d3));
}
public double mean(double... seeds) {
double sum = 0;
for (double seed : seeds) {
sum += seed;
}
return sum / seeds.length;
}
}