M
Marteno Rodia
Hello,
I encountered another problem today... I have an ArrayList<String> to
which I put String tokens. This takes place in a method which is to
return an array of these String tokens after parsing the input String
has been finished. Last line in the method:
return (String[]) tokenList.toArray();
throws: java.lang.ClassCastException: [Ljava.lang.Object; cannot be
cast to [Ljava.lang.String;
Why can't I do that? I've been putting only Strings to the ArrayList,
and the only reason why I must cast is that the standard toArray()
method returns array of Objects (not Strings)... And I have no idea
why the exception is thrown.
I encountered another problem today... I have an ArrayList<String> to
which I put String tokens. This takes place in a method which is to
return an array of these String tokens after parsing the input String
has been finished. Last line in the method:
return (String[]) tokenList.toArray();
throws: java.lang.ClassCastException: [Ljava.lang.Object; cannot be
cast to [Ljava.lang.String;
Why can't I do that? I've been putting only Strings to the ArrayList,
and the only reason why I must cast is that the standard toArray()
method returns array of Objects (not Strings)... And I have no idea
why the exception is thrown.