-4

I have a String abc="a","b","c"; I want to convert it to ArrayList. I am converting it using this code.

List<String> list11 = new ArrayList<String>(Arrays.asList(abc.split(" , ")));

Currently, output is coming like this

"subCategory": ["\"a\"", "\"b\"","\"c\""],`

I want to have output like this:

["a","b","c"]
GBlodgett
  • 12,704
  • 4
  • 31
  • 45
user3352615
  • 119
  • 2
  • 5
  • 13

2 Answers2

1

Given String abc="\"a\",\"b\",\"c\"";

You can simply replace the quotation marks within the String before using split():

 List<String> list11 = new ArrayList(Arrays.asList(abc.replace("\"", "").split(",")));

Output:

[a, b, c]
GBlodgett
  • 12,704
  • 4
  • 31
  • 45
  • `new ArrayList` seems superfluous here. Why would you want to make a mutable copy of an output that is already correct? – Dici Sep 24 '18 at 22:06
  • @Dici The OP said they wanted to convert it to an `ArrayList` – GBlodgett Sep 24 '18 at 22:07
  • Mmm, true, didn't see that. I maintain that it should only be done if a mutable output is required though (actually `Arrays.asList` returns a list that is not structurally mutable - can't add or remove elements - but is still modifiable - can re-assign an existing value - so it's already not immutable). – Dici Sep 24 '18 at 22:44
0

your string must be like below format so you can easily get ["a","b","c"]

String abc="\"a\""+","+"\"b\""+",\"c\"" ;
List list11 = new ArrayList(Arrays.asList(abc.split(" , ")));
System.out.println(list11);