my dog learned polymorphism*
The moose likes Beginning Java and the fly likes List to String array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "List to String array" Watch "List to String array" New topic
Author

List to String array

Anthony Smith
Ranch Hand

Joined: Sep 10, 2001
Posts: 285
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

Joined: Mar 22, 2002
Posts: 1055
The cast must match the object reference. Use
String [] descriptions = (String[])list.get(j);
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
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.


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: List to String array