This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes How to convert ArrayList to String Array in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to convert ArrayList to String Array in java" Watch "How to convert ArrayList to String Array in java" New topic
Author

How to convert ArrayList to String Array in java

satheesh krishnaswamy
Ranch Hand

Joined: Mar 17, 2004
Posts: 137
Hi,
I try to convert an ArrayList into String Array.
I am getting "java.lang.ClassCastException:Class cannot be casted to array".
I am sure that the ArrayList is not Null.

I tried like this.



I am getting a ClassCast Exception.Kindly Help.

Its Pretty Urgent,Please
Sunil Kumar Gupta
Ranch Hand

Joined: Aug 26, 2005
Posts: 824
Originally posted by satheesh krishnaswamy:
Hi,
I try to convert an ArrayList into String Array.
I am getting "java.lang.ClassCastException:Class cannot be casted to array".
I am sure that the ArrayList is not Null.

I tried like this.



I am getting a ClassCast Exception.Kindly Help.

Its Pretty Urgent,Please



Why dont you use toArray() method of ArrayList

String[] row = formattedRows.toArray();


Lack of will power has caused more failure than lack of intelligence or ability.
My Blog | Red5 Resources | Technology Update | Daily Technology Tips
satheesh krishnaswamy
Ranch Hand

Joined: Mar 17, 2004
Posts: 137
Hey,
Thanks for the immedieate reply.

String[] name = formattedRows.toArray(); will give us an Object Array and not a String array.

However String[] name = (String[])formattedRows.toArray() will be fine.

But then,how will I get the values of name now.

My requirement is,

My ArrayList will contain multiple rows and multiple values.
like [a[1,2],b[3,4]]

I need to set 1,2,3 and 4 into individual Values in a Value Object.

How do I do this?

Please help.
from multiple rows I need to get
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18916
    
  40

My ArrayList will contain multiple rows and multiple values.
like [a[1,2],b[3,4]]

I need to set 1,2,3 and 4 into individual Values in a Value Object.

How do I do this?


Unfortunately, all the toArray() method does is to create an array where each element is a member of the arraylist. If a member of the list is another arraylist object, or array, it will simply be one element of the array.

I don't believe there is a short cut to do what you want. You will have to write some code that will iterate through the array list, check each member, and do any further iteration if necessary.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to convert ArrayList to String Array in java