This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan 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 Android Security Essentials Live Lessons this week in the Android 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: 18545
    
  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)
 
Don't get me started about those stupid light bulbs.
 
subject: How to convert ArrayList to String Array in java
 
Similar Threads
String array to arraylist
java.Lang.classCastException
Problems in converting ArrayList to a Strin gArray
casting doubt
how to store map value in string array