allelopath
asked on
List<String> toArray problem
I have:
which caues the exception:
java.lang.ClassCastExcepti on: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String;
I've referred to nothing but String in myStringList, yet it think it is list of Object.
What gives?
List<String> myStringList = new ArrayList<String>();
then later:myStringList.add(myString);
then even later:return (String[]) myStringList.toArray();
which caues the exception:
java.lang.ClassCastExcepti
I've referred to nothing but String in myStringList, yet it think it is list of Object.
What gives?
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
:)
> The following is better. Otherwise (iirc) the array gets created twice
It doesn't get created twice
What CPColin suggested is the recommended approach
It doesn't get created twice
What CPColin suggested is the recommended approach
>>It doesn't get created twice
That's incorrect
That's incorrect
"It" isn't created twice, but two arrays are. The array passed in is created and the array returned is created. My suggestion is shorter, at the expense of the one extra initialization; CEHJ's is slightly more efficient, at the expense of a few more characters of code (although the cast shouldn't be necessary).
>>"It" isn't created twice, but two arrays are.
Yes, that's what i meant
Yes, that's what i meant
> at the expense of the one extra initialization
which is so small its not worth worrying about
> (although the cast shouldn't be necessary).
its not
which is so small its not worth worrying about
> (although the cast shouldn't be necessary).
its not
>>(although the cast shouldn't be necessary).
You're right - old habits die hard ;)
You're right - old habits die hard ;)
>>which is so small its not worth worrying about
How would you know how small that, created by reflection, would be?
How would you know how small that, created by reflection, would be?