Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should we use type cast for the object.toArray()?

Tags:

java

arrays

list

String[] a = c.toArray(new String[0]);

First: Do I need type cast here? (I think we should write like (String[])c.toArray(); BUT I have seen it as just c.toArray() without using type cast. Is this valid?

Second: Also why we write new String[0]?

like image 425
Johanna Avatar asked Jun 29 '09 05:06

Johanna


People also ask

What is the use of toArray () in Java?

The toArray() method of ArrayList is used to return an array containing all the elements in ArrayList in the correct order.

How do you use the toArray method?

The toArray() method of List interface returns an array containing all the elements present in the list in proper order. The second syntax returns an array containing all of the elements in this list where the runtime type of the returned array is that of the specified array.

What is the use of toArray in C#?

ToArray() Method in C# This method(comes under System. Collections namespace) is used to copy a Stack to a new array. The elements are copied onto the array in last-in-first-out (LIFO) order, similar to the order of the elements returned by a succession of calls to Pop.

How do you cast an object to an array of objects?

To convert an object to an array you use one of three methods: Object.keys() , Object.values() , and Object.entries() . Note that the Object.keys() method has been available since ECMAScript 2015 or ES6, and the Object.values() and Object.entries() have been available since ECMAScript 2017.


1 Answers

The type cast is usually only needed if you're using pre-generics Java. If you look at the docs for Collection.toArray(T[]) you'll see that it knows that the type of the array which is returned is the same as the type of array passed in. So, you can write:

List<String> list = new ArrayList<String>();
list.add("Foo");
String[] array = list.toArray(new String[0]);

You pass in the array to tell the collection what result type you want, which may not be the same as the type in the collection. For example, if you have a List<OutputStream> you may want to convert that to an Object[], a Stream[] or a ByteArrayOutputStream[] - obviously the latter is only going to work if every element actually is a ByteArrayOutputStream. It also means that if you already have an array of the right type and size, the contents can be copied into that instead of creating a new array.

A previous version of this answer was inaccurate, by the way - if you use the overload which doesn't take any parameters, you always get back an Object[] which can't be cast:

List<String> list = new ArrayList<String>();
list.add("Foo");

// toArray doesn't know the type of array to create
// due to type erasure
Object[] array = list.toArray();

// This cast will fail at execution time
String[] stringArray = (String[]) arrray;

EDIT: I've just noticed this is also mentioned in erdemoo's answer, but it can't hurt to have it here too :)

like image 149
Jon Skeet Avatar answered Nov 04 '22 08:11

Jon Skeet