简体   繁体   中英

Difference between Arrays and 3 dots (Varargs) in java

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.

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

  • An array is a fixed length collection of objects. eg new int[5];
  • An ArrayList is a variable length collection of objects. eg new ArrayList<Integer>();
  • The ... in variadic functions is a part of a method signature denoting an array of parameters. eg public void printLines(String... lines)

换句话说,method(String ...)意味着将可变数量的参数传递给方法。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM