It's called varargs. This fact should yield better Google results.
Answer from BalusC on Stack OverflowIt's called varargs. This fact should yield better Google results.
They 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.
Change the method definition to something as follows
public static void function(int number, List<String> listname) {
for (int i = 0; i < listname.size(); ++i) {
System.out.print(listname.get(i) + ": ");
}
System.out.println(number);
}
The Type should be a List<String> there is no standard LIST Type in Java (unless you make it ofcourse).
I’m going over test corrections and I don’t remember what this function header means:
public static void GyrosphereDinoList (String... list) {
//function definition
}
So, what does “...list“ mean in the function header?