How to convert an arrayList.toString() into an actual arraylist

China☆狼群 提交于 2020-07-16 02:26:52

问题


In my software, since there is no Array data type in SQLite, I saved my ArrayList as a String. Now I need to use my array and want to convert it back to an ArrayList. How can I do it?

Here an example :

ArrayList<String> list = new ArrayList<String>();
list.add("name1");
list.add("name2");
list.add("name3");
list.add("name4");
list.add("name5");
list.add("name6");

String newList = list.toString();
System.out.println(newList);

Result: [name1, name2, name3, name4, name5, name6]

So now how can I convert this into an ArrayList<String>?


回答1:


I believe this should work :

Arrays.asList(newList.substring(1, newList.length() - 1).replaceAll("\\s", "").split(","));
  • Take the string, remove the first and last bracket.
  • Remove each spaces.
  • Split by comma as delimiter, collect as list.

Note that if really you have to do this for a project, then there is something wrong in your code design. However, if this is just for curiosity purpose then this solution would work.


After testing

ArrayList<String> list = new ArrayList<String>();
list.add("name1");
list.add("name2");
list.add("name3");
list.add("name4");
list.add("name5");
list.add("name6");

String newList = list.toString();                
List<String> myList = Arrays.asList(newList.substring(1, newList.length() - 1).replaceAll("\\s", "").split(","));

System.out.println(myList);

would compile properly and print :

[name1, name2, name3, name4, name5, name6]

Edit

As per your comments, if really you want your variable to be an ArrayList<String> instance then you could pass the list to ArrayList constructor :

ArrayList<String> myList = new ArrayList(Arrays.asList(newList.substring(1, newList.length() - 1).replaceAll("\\s", "").split(",")));

You can't cast directly as Arrays.asList use it own builtin java.util.Arrays$ArrayList class.




回答2:


This is not possible to do without ambiguity. Consider:

ArrayList<String> list = new ArrayList<String>();
list.add("name1, name2");
list.add("name3, name4");
list.add("name5");
list.add("name6");

String newList = list.toString();
System.out.println(newList);

Result: [name1, name2, name3, name4, name5, name6]

In order to accurately recover the original elements in the general case, your string format must be smarter than ArrayList.toString(). Consider a pre-defined way of encoding lists of strings, perhaps a JSON array, which would result in something like this for my example:

["name1, name2", "name3, name4", "name5", "name6"]

JSON also defines how to handle strings with quotes via escaping and/or use of alternate string start/end characters ' and ":

["he said, 'hello'", 'she said, "goodbye"', 'I said, "it\'s raining"']

(I also agree with other commenters that your database design should be reconsidered, but wanted to provide a clear answer illustrating the issues with string encodings of lists.)




回答3:


Then it the method that only accepts strings would be able to add a case where something like this were passed in?

methodThatOnlyAllowsStrings((Object)list);


来源:https://stackoverflow.com/questions/29618139/how-to-convert-an-arraylist-tostring-into-an-actual-arraylist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!