Possible Duplicate:
why does List<String>.toArray() return Object[] and not String[]? how to work around this?
See this code example :
List<String> list = new ArrayList<String>();
list.add("hello");
list.add("world");
Object [] array = list.toArray();
Why does the method toArray()
return an array of Object
? Other List
methods such as get()
, remove()
are able to return the type of objects in the list.
ToArray which in turn creates copy of elements (optimized for ICollection and hence Array[] , but still making copy) with internal Buffer class.
The toArray() method of ArrayList is used to return an array containing all the elements in ArrayList in the correct order.
Because array has been in Java since the beginning, while generics
were only introduced in Java 5. And the List.toArray() method was
introduced in Java 1.2, before generics existed, so it was
specified to return Object[].
u can use toArray(T[] a)
just take a look Collection.toArray(T[] a)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With