List<String> toArray problem

I have:
 List<String> myStringList = new ArrayList<String>();

Open in new window

then later:
myStringList.add(myString); 

Open in new window

then even later:
return (String[]) myStringList.toArray();

Open in new window


which caues the exception:
java.lang.ClassCastException: [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?
LVL 1
allelopathAsked:
Who is Participating?
 
CEHJConnect With a Mentor Commented:
The following is better. Otherwise (iirc) the array gets created twice



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

Open in new window

0
 
for_yanConnect With a Mentor Commented:
It works this way:

                    String myString = "ttt";
        List<String> myStringList = new ArrayList<String>();
        myStringList.add(myString);
        Object [] aS =  (Object []) myStringList.toArray();
        String [] as1 = new String[aS.length];
        for(int j=0; j<aS.length; j++){
            as1[j] = (String) aS[j];
        }

I guess you can't case Object [] to String [] in one shot - you need to do it one by one


0
 
CPColinConnect With a Mentor Senior Java ArchitectCommented:
It should work if you pass an empty String array to the toArray() method, like this:

return myStringList.toArray(new String[0]);

Open in new window


http://download.oracle.com/javase/6/docs/api/java/util/List.html#toArray%28T[]%29
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
CEHJCommented:
:)
0
 
objectsCommented:
> The following is better. Otherwise (iirc) the array gets created twice

It doesn't get created twice
What CPColin suggested is the recommended approach
0
 
CEHJCommented:
>>It doesn't get created twice

That's incorrect
0
 
CPColinSenior Java ArchitectCommented:
"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).
0
 
CEHJCommented:
>>"It" isn't created twice, but two arrays are.

Yes, that's what i meant
0
 
objectsCommented:
> 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
0
 
CEHJCommented:
>>(although the cast shouldn't be necessary).

You're right - old habits die hard ;)
0
 
CEHJCommented:
>>which is so small its not worth worrying about

How would you know how small that, created by reflection, would be?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.