Could someone show me a practical a use of the ... array method delcaration? Is it just a way to declare an optional parameter instead of passing null value?
public void add(int a, int... b) {
// do something
}
add(1);
add(1,2,3);
Could someone show me a practical a use of the ... array method delcaration? Is it just a way to declare an optional parameter instead of passing null value?
public void add(int a, int... b) {
// do something
}
add(1);
add(1,2,3);
It's a shorthand for you when you're writing code that will use a function that can take an array as a parameter.
It's generally easier to write add(1,2,3,4); than it is to write add(new int[] {1,2,3,4});, right? It's also clearer when it needs to be read and maintained by future programmers later.
Think about it this way: which function would you call, the one where you have to create an array every time, or the one where you can just pass in as many parameters as you want?
The Formatter class is such a practical use:
Formatter formatter = new Formatter(sb, Locale.US);
formatter.format("%4$2s %3$2s %2$2s %1$2s", "a", "b", "c", "d")
An arbitrary number of parameters can be passed in to be formatted using the first parameter, which is the format string itself.
Derived from you example you could make a method to have the sum of all the ints you pass to the method:
public int sum(int a, int... b) {
int sum = a;
for (int i : b) {
sum += i;
}
return sum;
}
String.format is a pretty good practical example. The method doesn't know how many formatters will appear in the format string, so it accepts a variable number of arguments, and there should be a one-to-one mapping of formatters (in the format string) to objects passed into the method.
It's not so much a way of declaring an option parameter rather than null, as it is to declare a method that can deal with multiple arguments. Basing my example off of mine, you could write a sum method that takes a variable number of arguments and sums them all together:
public int sum(int... ns)
{
int sum = 0;
for (int n : ns) {
sum += n;
}
return sum;
}
That way, you could pass in 2, 3, 4, or even 100 numbers to sum, depending on your need at the time.
Before varargs a method that took an arbitrary number of values required you to create an array put the values into the array prior to invoking the method. Example here is how the MessageFormat class to format a message used to look:
Object[] arguments = {
new Integer(7),
new Date(),
"a disturbance in the Force"
};
String result = MessageFormat.format(
"At {1,time} on {1,date}, there was {2} on planet "
+ "{0,number,integer}.", arguments);
It is still true that multiple arguments must be passed in an array, but the varargs feature automates and hides the process. Furthermore, it is upward compatible with preexisting APIs. So, for example, the MessageFormat.format method now has this declaration:
public static String format(String pattern,
Object... arguments);
The three periods after the final parameter's type indicate that the final argument may be passed as an array or as a sequence of arguments. Varargs can be used only in the final argument position. Given the new varargs declaration for MessageFormat.format, the above invocation may be replaced by the following shorter and sweeter invocation:
String result = MessageFormat.format(
"At {1,time} on {1,date}, there was {2} on planet "
+ "{0,number,integer}.",
7, new Date(), "a disturbance in the Force");
So when should you use varargs? As a client, you should take advantage of them whenever the API offers them. Important uses in core APIs include reflection, message formatting, and the new printf facility. As an API designer, you should use them sparingly, only when the benefit is truly compelling. Generally speaking, you should not overload a varargs method, or it will be difficult for programmers to figure out which overloading gets called.
From: http://java.sun.com/j2se/1.5.0/docs/guide/language/varargs.html