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

前端 未结 3 1744
无人及你
无人及你 2021-01-20 14:06

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

相关标签:
3条回答
  • 2021-01-20 14:28

    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.

    0 讨论(0)
  • 2021-01-20 14:36

    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);
    
    0 讨论(0)
  • 2021-01-20 14:42

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

    0 讨论(0)
提交回复
热议问题