tags:

views:

705

answers:

4
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]?

+1  A: 
  1. Yes you need the downcast since toArray's returns type is Object[].

  2. You need to pass (new String[0]) as a parameter since the method needs to know what kind of array it should return (array of strings, Dates, etc.) Internally all list elements are actually objects so the list does not know the type of elements it is holding and therefore it does not know which kind of array it should return, unless you provide it as a parameter.

Itay
good,but I think we need upcast here!!!
Johanna
A: 

To add to Itay's response, the parameter-less ToArray method of the ArrayList class returns an array of type Object.

Try string[] array = c.ToArray(typeof(string)) as string[];

Saif Khan
This is Java, not C#.
Jon Skeet
if I use typeof(String) it will return what instead of null???
Johanna
+5  A: 

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 :)

Jon Skeet
Can we say that I write new String[0] for not returning null as the result????
Johanna
(Clarified and corrected). I don't really understand the comment though Johanna - could you explain what you mean a bit more?
Jon Skeet
is it true that if the elements of the array is more than the collection,we can use **new String[0]** ??
Johanna
What do you mean? You can always use "new String[0]" and it will create a new array for you of the appropriate size.
Jon Skeet
+1  A: 

if you are using list.toArray(), it will return you Object array. Casting to String array will throw exception even if elements stored in list are String type.

if you are using list.toArray(Object[] a), it will store elements in list to "a" array. If the elements inside the list are String and you give String array then it will store elements inside String array, if given array is not large enough to store elements inside the list, then it will expand given list.

erdemoo