Difference between Arrays and 3 dots (Varargs) in java

Java

Java Problem Overview


I cannot figure out that what are the differences between ... in java and arrays also array list, especially array list.

Both we can use as unlimited but ... is rarely used.

Please help thanks in advance.

Java Solutions


Solution 1 - Java

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");

Solution 2 - Java

  • An array is a fixed length collection of objects. e.g. new int[5];

  • An ArrayList is a variable length collection of objects. e.g. new ArrayList<Integer>();

  • The ... in variadic functions is a part of a method signature denoting an array of parameters. e.g. public void printLines(String... lines)

Solution 3 - Java

In other words, method(String...) means passing a variable number of parameters to the method.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1550048View Question on Stackoverflow
Solution 1 - JavaJornView Answer on Stackoverflow
Solution 2 - JavatskuzzyView Answer on Stackoverflow
Solution 3 - JavaAliaksandr DvoineuView Answer on Stackoverflow