Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

List to String array

 
Anthony Smith
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have taken several String[] and stored them into a ArrayList. Now in order to access this list to get each String[] how do I do it?
UN.descriptions, where UN is my object and descriptions is my ArrayList.
I have a method called getDescriptions that returns an ArrayList.
I thought I could do:
for(int j=0; j!=list.size(); j++)
{
String [] descriptions = (String)list.get(j);
out.print("<br>!!!" + descriptions[0]);
out.print("<br>@@@" + descriptions[1]);
}
****************
String [] descriptions = (String)list.get(j);
It bombs on this statement. Cant convert Can't convert java.lang.String to java.lang.String[].
---->How can I get my String array?
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The cast must match the object reference. Use
String [] descriptions = (String[])list.get(j);
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String and String[] are different types and you cannot cast one to the other.
If you store something in an ArrayList, when you take it out, you need to cast it to the same type you put in. Decide whether it should be String or String[], but it can only be one of these.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic