Need to figure out what is the difference between ...
and arrays in JAVA, also an array list.
It seems we can use both as unlimited, but ...
is rarely used.
Really appreciate any help you can.
The three dots can only be used in a method argument, and are called 'varargs'. It means you can pass in an array of parameters without explicitly creating the array.
private void method(String[] args) {}
is called like method(new String[]{"first", "second"});
private void method(String... args) {}
is called like method("first", "second");
method(String... args)
can be invoked as method("first", "second")
, but it can just as well be invoked as method(new String[]{"first", "second"})
.
new int[5];
ArrayList
is a variable length collection of objects. e.g. new ArrayList<Integer>();
...
in variadic functions is a part of a method signature denoting an array of parameters. e.g. public void printLines(String... lines)
In other words, method(String...) means passing a variable number of parameters to the method.