Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java main methods with "..." arrays? [duplicate]

Possible Duplicate:
What is the ellipsis (…) for in this method signature?
java: how can i create a function that supports any number of parameters?

well i'm trying to figure out some examples and I found this kind of array definition for arguments in the main method. What's so special about this "..." and what's the difference between a normal String[] args?

Thanks

like image 827
Arturas M Avatar asked May 03 '12 01:05

Arturas M


People also ask

Which method of an array creates a duplicate copy?

Use the clone method of the array.

Does array allow duplicates in Java?

ArrayList is backed by an Array while HashSet is backed by an HashMap. Duplicates : ArrayList allows duplicate values while HashSet doesn't allow duplicates values.

What is clone method in an array?

The references in the new Array point to the same objects that the references in the original Array point to. In contrast, a deep copy of an Array copies the elements and everything directly or indirectly referenced by the elements. The clone is of the same Type as the original Array.


2 Answers

That's a notation from Java 5 for variable length argument lists. It is roughly equivalent to a String array, but lets you pass individual parameters that are combined into an array automatically.

void mymethod(String... a) {
    for (String s : a) {
        ...
    }
}

mymethod("quick", "brown", "fox");

This makes sense only when you plan to call a method from your program, so I do not see why it would be desirable to use this syntax for your main(). It will work, however.

like image 154
Sergey Kalinichenko Avatar answered Sep 21 '22 22:09

Sergey Kalinichenko


The elipsis (...) are varargs. They are used when you want to create a method with any number of arguments. Oracle explains how varargs work in detail here.

like image 22
Alex Lockwood Avatar answered Sep 17 '22 22:09

Alex Lockwood