0

I have the printed value of an ArrayList of Strings.

[a, b, c, d]

This will be in String format.

Eg.

String temp = "[a, b, c, d]"

How can I convert this into an ArrayList of String object?

1 Answers1

0

Very simple code

String temp = "[a, b, c, d]";

// trim enclosing brackets and then split by comma and space
String[] array = temp.substring(1, temp.length() - 1).split(", ");

List<String> list = new ArrayList<String>();

for (String s : array) {
    list.add(s);
}
Braj
  • 46,415
  • 5
  • 60
  • 76
  • I'm assuming there aren't any in-built functions to do this – user2324943 May 05 '14 at 06:51
  • Sorry There are infinite no of problems in the JAVA world :) – Braj May 05 '14 at 06:51
  • Could you also help me in one thing, similarly, if I have contents of a Stack in a String, and want to reconvert to an Object of Stack type, how could I do that? I would have to read the string in a reverse order? – user2324943 May 05 '14 at 06:52
  • Where is the issue? Just code and code to make you perfect. Try to experiment on code. It's very easy. – Braj May 05 '14 at 06:56