java_mouse
java_mouse

Reputation: 2109

ArrayList.toArray() method in Java

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.

Upvotes: 10

Views: 14901

Answers (7)

Chandra Sekhar
Chandra Sekhar

Reputation: 19500

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.

Upvotes: 0

Mathias Schwarz
Mathias Schwarz

Reputation: 7197

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.

Upvotes: 2

Ashok Raj
Ashok Raj

Reputation: 454

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

See this documentation

Upvotes: -1

Sanjay T. Sharma
Sanjay T. Sharma

Reputation: 23248

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.

Upvotes: 0

Louis Wasserman
Louis Wasserman

Reputation: 198591

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.

Upvotes: 1

Andrzej Doyle
Andrzej Doyle

Reputation: 103847

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.

Upvotes: 14

Churk
Churk

Reputation: 4637

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[])

Upvotes: 0

Related Questions