File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes List to String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "List to String" Watch "List to String" New topic

List to String

Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1283

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...


No Kaustubh No Fun, Know Kaustubh Know Fun..
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46375
Use a regular expression and String#split to remove the hashcode?
Matthew Brown

Joined: Apr 06, 2010
Posts: 4543

You need to get the entire list as a String? Or just some of the values?

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

Joined: Sep 16, 2008
Posts: 241
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.

I no good English.
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1283

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

I agree. Here's the link:
subject: List to String
It's not a secret anymore!