The reason is simple: a variable arity parameter is simply an old-school array paramater with some additional metadata that tells the compiler to provide some syntactic sugar (namely, it allows implicit array creation).
So from the perspective of the JVM Object...
is pretty much the same as Object[]
. Allowing collections as well would require a more invasive change to the JVM (which has no explicit support for collections to date).
Note that if you want to support both ways, then making the collection-based method is probably the better approach:
public void frobnicate(Object... args) {
frobnicate(Arrays.asList(args));
}
public void frobnicate(Iterable<Object> args) {
// do stuff
}
The reason for this is that using Arrays.asList()
is usually a cheaper operation than Collection.toArray()
(because it creates a simple wrapper).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…