• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to convert ArrayList to String Array in java

 
satheesh krishnaswamy
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 824
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
 
satheesh krishnaswamy
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20881
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic