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.

5 Answers 5

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

Sign up to request clarification or add additional context in comments.

4 Comments

yes. but why that above not working. I just need to know why we can't use that.
Who downvoted? And why? I really want to learn from my mistakes. Don't downvote without mentioning why.. That's a very bad habit.
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..
@vidudaya That's why I wrote "I'm editing.." :) I'm glad it helped you.
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[]{});

Comments

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

Comments

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,

2 Comments

If you specify the type in the parameter, you don't need to cast it! :)
@R.J Argh, you're right of course - that's what copypasting from the question will do to you...
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

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.