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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

for_yanCommented:
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
CPColinSenior 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
CEHJCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

CEHJCommented:
:)
0
Mick BarryJava DeveloperCommented:
> 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
Mick BarryJava DeveloperCommented:
> 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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.