This week's book giveaway is in the OCP forum.
We're giving away four copies of OCP Java SE 8 Programmer II Exam Study Guide and have Kathy Sierra, Bert Bates, & Elizabeth Robson on-line!
See this thread for details.
Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Is it possible to get Arraylist from String.  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have just pasted my code over here.Can anyone of you find the solution for obtaining the Array list from the String.

For Example :

ArrayList al = new ArrayList();
al.add(new byte[2]);
al.add("2");

String stringVar = "" + (ArrayList) al;

Here I can convert the arraylist into string.But can we do he vice versa like obtaining the above arraylist from the string?If please advice me.

URGENT!!!
 
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not an advanced question. Moving...
 
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you want to put the String into the ArrayList? Character by Character? Is the String formatted in a similar fashion to what gets returned by the toString method in AbstractCollection, which is inherited by ArrayList?
 
Ranch Hand
Posts: 136
1
Netscape Opera Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by rahul reddy:
Hi all,

I have just pasted my code over here.Can anyone of you find the solution for obtaining the Array list from the String.

For Example :

ArrayList al = new ArrayList();
al.add(new byte[2]);
al.add("2");

String stringVar = "" + (ArrayList) al;

Here I can convert the arraylist into string.But can we do he vice versa like obtaining the above arraylist from the string?If please advice me.

URGENT!!!



You can use StringTokenizer to parse or tokenize a string and add it to an ArrayList. I believe StringTokenizer has some deficiencies in that it can't handle "100 North Main Street, #110", New York, NY - that is, a delimiter in the data. But you do get to choose your delimiter.

This example builds an ArrayList, converts it to a string, and puts it back into another array list. NOTE: because I created the string from ArrayList.toString() I had to add extra logic to remove the [] that ArrayList adds when converting to a String.


[ August 07, 2007: Message edited by: Red Smith ]
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!