ArrayList to an Array

I am trying to change an ArrayList to an Array. I have tried the following code:

ArrayList myList=new ArrayList();

// Added some things here.

String[ ] myArray = myList.toArray(new String[0]);

When I try this I get an error saying "Cannot find symobl: variable String.

What am I doing wrong.

[426 byte] By [jdub53a] at [2007-11-27 9:48:39]
# 1

String[ ] myArray = myList.toArray(new String[]{});

new String[]{} creates a new empty String array.

String[0] refers to the first element of an array variable called String, which doesn't exist, and saying new in front of that just blows the compiler's mind.

hunter9000a at 2007-7-13 0:17:15 > top of Java-index,Java Essentials,Java Programming...
# 2
Now I get an incompatible types error: Incompatible types: found: java.lang.Object[ ]required: java.lang.String[ ]
jdub53a at 2007-7-13 0:17:15 > top of Java-index,Java Essentials,Java Programming...
# 3
Huh? Works for me, with no head explosions:ArrayListExampleList < String > myList = new ArrayList < String > ();String[] array = myList.toArray(new String[0]);
BigDaddyLoveHandlesa at 2007-7-13 0:17:15 > top of Java-index,Java Essentials,Java Programming...
# 4
> Now I get an incompatible types error: > > Incompatible types: > > ound: java.lang.Object[ ]> required: java.lang.String[ ]Can you use generics? See previous post.
BigDaddyLoveHandlesa at 2007-7-13 0:17:15 > top of Java-index,Java Essentials,Java Programming...
# 5
I was missing the <String> tags. The tags got it to work. Thank you for all your help.
jdub53a at 2007-7-13 0:17:15 > top of Java-index,Java Essentials,Java Programming...
# 6

> I was missing the <String> tags. The tags got it to

> work. Thank you for all your help.

BTW, here's two tutorials on generics:

http://java.sun.com/docs/books/tutorial/java/generics/index.html

http://java.sun.com/docs/books/tutorial/extra/generics/index.html

BigDaddyLoveHandlesa at 2007-7-13 0:17:15 > top of Java-index,Java Essentials,Java Programming...
# 7
Of course, if you are not using Generics, you can still convert an ArrayList to an array.ArrayList list = new ArrayList();String [] arr = (String[])(list.toArray(new String [list.size()]));~Tim
SomeoneElsea at 2007-7-13 0:17:15 > top of Java-index,Java Essentials,Java Programming...