I don't understand that syntax. Trying to google various words plus "..." is useless.
TIA
I don't understand that syntax. Trying to google various words plus "..." is useless.
TIA
It's a java method that took an arbitrary variable number of parameters
They are variable length parameters.
Here is one link with an example.
As @BalusC mentioned, it's a varags parameter. This means you can pass a variable number of arguments to that method.
So for a method defined as
public void foo(String...strings) { }
The following invocations are legal:
foo();
foo("one param");
foo("one", "two", "three");
It means "Arbitrary number of arguments" meaning you can pass a unknown number of parameters into the method..
see
http://download.oracle.com/javase/tutorial/java/javaOO/arguments.html Look for the "Arbitrary number of arguments" section
public Polygon polygonFrom(Point... corners) {
int numberOfSides = corners.length;
double squareOfSide1, lengthOfSide1;
squareOfSide1 = (corners[1].x - corners[0].x)*(corners[1].x - corners[0].x)
+ (corners[1].y - corners[0].y)*(corners[1].y - corners[0].y) ;
lengthOfSide1 = Math.sqrt(squareOfSide1);
// more method body code follows that creates
// and returns a polygon connecting the Points
}
This is called Variadic function (wiki page with examples in many languges).
In computer programming, a variadic function is a function of indefinite arity, i.e. one which accepts a variable number of arguments. Support for variadic functions differs widely among programming languages. There are many mathematical and logical operations that come across naturally as variadic functions. For instance, the summing of numbers or the concatenation of strings or other sequences are operations that can logically apply to any number of operands. Another operation that has been implemented as a variadic function in many languages is output formatting. The C function printf and the Common Lisp function format are two such examples. Both take one argument that specifies the formatting of the output, and any number of arguments that provide the values to be formatted. Variadic functions can expose type-safety problems in some languages. For instance, C's printf, if used incautiously, can give rise to a class of security holes known as format string attacks. The attack is possible because the language support for variadic functions is not type-safe; it permits the function to attempt to pop more arguments off the stack than were placed there -- corrupting the stack and leading to unexpected behavior. Variadic functionality can be considered complementary to the apply function, which takes a function and a list/sequence/array as arguments and then calls the function once, with the arguments being the elements of the list.
One of may personal favorite not used features in Java. It is basically a reference array that is built from elements. One of the best ways to use it is on class constructor, or method where you need to constantly find a value like maximum of 2, 3, 4, 5 input elements.
One example is, when i built a generic binary tree node
, for coding tasks, I used this in constructor. This enabled me simply add elements to the tree and distribute them.
Following creates String type binary tree, with root "Wein"
and 2 branches "Eric"
and "Paul"
.
new MBTN<String>("Wein", "Eric", "Paul").
Could you think what the alternatives would be :D You can't even simply make generic array of elements, so this would stretch like hell. It is definitely not useless.
As mentioned by everyone...variable arguments (or varargs) allows you to do this....
//Method we're using for varargs
public void doSomething(String... datas) {
if (datas == null || datas.length == 0) {
System.out.println("We got nothing");
} else {
for (String data: datas) {
System.out.println(data);
}
}
}
Therefore, all these calls mentioned below are valid....
String d[] = {"1", "2", "3"};
doSomething(d); //An array of String, as long as the type is exactly as the varargs type.
//OR
doSomething("1", "2", "3", "4"); //I can add "infinitely" many arguments as the JVM can allocate me....
//OR
doSomething("1"); //Exactly 1;
Internally varargs is "essentially" a reference array of it's declared type.
The are the "variable arguments" or varargs (for short).
Basically it allows the passing of an unspecified number of Strings, so the method signature
public void printStuff(String...messages)
Effectively can handle the following calls
printStuff("hi");
printStuff("hi", "bye");
printStuff("Hello", "How are you?", "I'm doing fine.", "See you later");
You can effectively consider this a type of autoboxing. The printStuff
argument can be seen as an array, so printStuff(String...messages)
is conceptually handled like printStuff(String[] messages)
. Wtih the calls above effectively acting like
printStuff(new String[] {"hi"});
printStuff(new String[] {"hi", "bye"});
printStuff(new String[] {"Hello", "How are you?", "I'm doing fine.", "See you later"});
To access the messages internally, you use typical List
handling primitives. Something like
...
if (messages != null) {
for (String message : messages) {
System.out.println(message);
}
}
...
That there is no need to actually create arrays is a bit of syntactic sugar added to Java with the advent of auto boxing.