2

Why the below code fail to execute though it wont detect as an error from the IDE. And it will compile fine.

 ArrayList<String> a = new ArrayList<String>();
    a.add("one");
    a.add("two");
    a.add("three");
    String [] b = (String[])a.toArray();
    for(int i =0;i<b.length;++i){
        System.out.println(b[i]);
    }

But it will give the following error.

nested exception is java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String;

Can anyone give a clear explanation? The same problem has been asked before and some solutions has been provided. But a clear explanation of the problem will be much appreciated.

Maroun
  • 87,488
  • 26
  • 172
  • 226
prime
  • 11,246
  • 11
  • 75
  • 112

5 Answers5

5

You should simply do:

String[] b = new String[a.size()];
a.toArray(b);

You're getting the error because toArray() returns Object[] and this cannot be cast down to String[].

Maroun
  • 87,488
  • 26
  • 172
  • 226
  • yes. but why that above not working. I just need to know why we can't use that. – prime Nov 21 '13 at 11:06
  • 2
    Who downvoted? And why? I really want to learn from my mistakes. Don't downvote without mentioning why.. That's a very bad habit. – Maroun Nov 21 '13 at 11:09
  • FYI its not me. And it's because you first gave the way to convert the list to String array and that is not the expected answer to this question. And then you edited the rest. It took 2+ mins and i think in stackoverflow its a reasonable time. So don't get angry :) your answer is great. thanks for that. :) Cheers.. – prime Nov 21 '13 at 11:13
  • @vidudaya That's why I wrote "I'm editing.." :) I'm glad it helped you. – Maroun Nov 21 '13 at 11:14
3

You need to mention the type of array, else by default, toArray() would return an array of Object which can't be simply casted to String[]. If you specify the type, the overloaded toArray(T[]) would be called, returning the type of array mentioned as the parameter.

String [] b = a.toArray(new String[]{});
RainMaker
  • 41,717
  • 11
  • 78
  • 97
3

a.toArray() is creating an Object[] rather than a String[] and hence the typecast is failing.

 String[] b = a.toArray(new String[a.size()]);

Refer to the javadocs for the two overloads of List.toArray

Prateek
  • 6,239
  • 2
  • 22
  • 37
0

Have a look at the JavaDoc, toArray() returns a Object[] array, which cannot be downcast to String[].

You'll need this method instead - the reason being that generics are erased at runtime, so the JVM won't know that your ArrayList used to be one that contained String's:

String [] b = a.toArray( new String[] {} );

Cheers,

Anders R. Bystrup
  • 14,996
  • 10
  • 58
  • 53
0

(Something that might help in now/future.)

Since 1.5 you are are able to do this way:

for(String output : a) { // Loops through each a (which is a String)
 System.out.println(output); // As it is a String list we can just print it
}

This more more readable and may come in handy to know.

Output:

one
two
three
Peter_James
  • 657
  • 4
  • 13
  • 1
    yeah.. But i did that printing stuff just for checking. :) and thanks for you idea :) (actually my question was not how to print the list elements) :) – prime Nov 21 '13 at 11:20