• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

List to String

 
Kaustubh G Sharma
Ranch Hand
Posts: 1283
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I am stuck in a palce where the values are in a List and i have to save this value into a string

I tried list.toString(); //not give the string i am looking 4 means it includes hashcode also
(String) list.get(index) //gives me a class cast exception...

Any other way out to solve this problemo...

Kaustubh
 
Campbell Ritchie
Sheriff
Posts: 48377
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a regular expression and String#split to remove the hashcode?
 
Matthew Brown
Bartender
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to get the entire list as a String? Or just some of the values?

list.get(index).toString()
will convert any of the values to a String (whether that's useful will depend on what sort of objects they are).
 
Martin Vanyavchich
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A code sample would be nice

Not sure where're you getting that hash code from.


prints out: [octopus, fish, shark]

If you have objects in that list of your own creation, override toString method of your class.
 
Kaustubh G Sharma
Ranch Hand
Posts: 1283
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks guys for the knowledge actually using user's and little mine apporoch I come up with this code...


List list= mhome.fetchMsgIdentifierByMessageId(5);
Object obj[] = (Object[]) list.get(0);
String s1=obj[0].toString();

it really works

Thanks
Kaustubh
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic