10

I am wondering why did they design the toArray method in ArrayList to take a input of an array in Java?

    ArrayList<String> listArray = new ArrayList<String>();

    listArray.add("Germany");
    listArray.add("Holland");
    listArray.add("Sweden");

    String []strArray = new String[3];
    String[] a = (String [])listArray.toArray(strArray);

To me it appears that, they dont need this input because the instance of the ArrayList itself has enough details to convert the data into an array.

My question is why do they still need the array to be passed in? Thanks.

java_mouse
  • 2,069
  • 4
  • 21
  • 30

7 Answers7

14

Two reasons I can think of:

  1. Erasure means that the generic parameters aren't available at runtime, so an ArrayList<String> doesn't know that it contains strings, it's just the raw type ArrayList. Thus all invocations of toArray() would have to return an Object[], which isn't strictly correct. You'd have to actually create a second array of String[] then iterate over the first, casting all of its parameters in turn to come out with the desired result type.
  2. The way the method is defined means that you can pass in a reference to an existing array, and have this populated via the method. In some cases this is likely very convenient, rather than having a new array returned and then copying its values elsewhere.
Andrzej Doyle
  • 102,507
  • 33
  • 189
  • 228
  • Thanks, I understood. In my example, should I assume "strArray" or "a" as the final result? Are they same? – java_mouse Mar 26 '12 at 14:51
  • 4
    They're the same, _if_ `collection.size() <= strArray.size()`, which is true in your specific case. It's generally best practice to use `a` anyway. – Louis Wasserman Mar 26 '12 at 15:09
  • Plus 1 (as +1 is forbidden in comments) : for bringing the context of type erasure in this – manifold Jul 18 '16 at 13:32
2

In your code, the ArrayList can contain anything, not only Strings. You could rewrite the code to:

ArrayList<String> listArray = new ArrayList<String>();

listArray.add("Germany");
listArray.add("Holland");
listArray.add("Sweden");

String []strArray = new String[3];
String[] a = listArray.toArray(strArray);

However, in Java arrays contain their content type (String) at runtime, while generics are erased by the compiler, so there is still no way for the runtime system to know that it should create a String[] in your code.

Mathias Schwarz
  • 7,099
  • 23
  • 28
1

You need it to get array types other than Object[]. listArray.toArray(new String[3]) shouldn't actually need a cast to String[], but it lets you write into arrays for specific types, rather than the (rather unsafe) approach of just casting Object[] to whatever your type is.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
0

It is so that when you have an non-empty array you want object to be appended to from the arraylist conversion you can do that without having to create a new array, and do post processing of merging it, save resource and extra work.

Read the second paragraph of the javadoc: http://docs.oracle.com/javase/1.5.0/docs/api/java/util/ArrayList.html#toArray(T[])

Churk
  • 4,556
  • 5
  • 22
  • 37
0

It is not compulsory to pass an Array as an argument to toArray(), you can directly call ArrayList.toArray()

As we know that Collection framework with generics works with the concept of Type Erasure. So the ArrayList becomes List of Object, so when you convert to array it will give an Object[] if you don't pass any argument.

Community
  • 1
  • 1
Chandra Sekhar
  • 18,914
  • 16
  • 84
  • 125
0

ArrayList doesn't have enough information given that the "data" itself is stored in buckets of Object arrays. So, if you know "what" kind of items you have in your array list, you can achieve type safety by using this variant of the toArray method.

Sanjay T. Sharma
  • 22,857
  • 4
  • 59
  • 71
-1

Because the array into which the elements are to be copied should be specified to the JVM.

See this documentation

Ashok Raj
  • 444
  • 6
  • 25