Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

ArrayList.toArray() behaviour in JDK 1.5

807607Nov 24 2006 — edited Nov 24 2006
Hello,
Can someone tell me what is wrong with following code

public static void main(String[] args){
ArrayList<String> a = new ArrayList();
a.add("one");
a.add("twp");

String[] array = a.toArray();

// Even this one does not work and gives a class cast exception
String[] array = (String[]) a.toArray();

for (int i=0;i<array.length;i++)
System.out.println(array);

}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 22 2006
Added on Nov 24 2006
2 comments
167 views