Varargs are useful for any method that needs to deal with an indeterminate number of objects. One good example is String.format
. The format string can accept any number of parameters, so you need a mechanism to pass in any number of objects.
String.format("This is an integer: %d", myInt);
String.format("This is an integer: %d and a string: %s", myInt, myString);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…