This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
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

Win a copy of REST with Spring (video course) this week in the Spring forum!
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: 45292
Use a regular expression and String#split to remove the hashcode?
Matthew Brown

Joined: Apr 06, 2010
Posts: 4541

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!