3

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>?

12
  • 1
    I don't think you want to use toString, but toArray instead Apr 14, 2015 at 2:30
  • 1
    will list as blob in sql be a bad option ? Apr 14, 2015 at 2:32
  • 3
    If any of your strings contain commas, the toString() conversion is lossy and there is no simple reliable way to recover the original array.
    – Boann
    Apr 14, 2015 at 2:42
  • 3
    You should beware with using toString() for serialisation. If you add "firstname, lastname" to your list it is impossible to know if it should be deserialised as ["firstname, lastname"] or ["firstname", "lastname"] since toString() will output "[firstname, lastname]" for both.
    – Raniz
    Apr 14, 2015 at 2:44
  • 1
    Sort of, you have one table for your main objects (those that contain the lists) and one that contains all items in all lists as separate rows. Each row in the second table contains a reference to the row in the first table where it's owning object is stored (and it's index in the list if order is important). To re-assemble the object you first load the object from the first table and then all it's list items from the second table.
    – Raniz
    Apr 14, 2015 at 2:59

3 Answers 3

4

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
  • Thanks for your answer. How can I have the output as an ArrayList<String> ?
    – Dan
    Apr 14, 2015 at 2:40
  • @user2357112 Rollbacked to my previous version. Actually when seeing the other answer, I tought it allowed this possibility which is obviously not the case. Apr 14, 2015 at 2:56
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.)

1
  • Thanks Indeed. That's a good idea. I guess GSON from Google can do this. I appreciate it.
    – Dan
    Apr 14, 2015 at 3:36
-1

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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.