简体   繁体   English

附加到 StringBuilder 后如何将项目添加回 ArrayList?

[英]How to add items back to an ArrayList after appending to StringBuilder?

ArrayList<String> itemslist = new ArrayList<>();
StringBuilder stringBuilder = new StringBuilder();
for (String item : itemslist) {
     stringBuilder.append(item);
     stringBuilder.append("\n");
}
String textArray = stringBuilder.toString();

What is the way to create an ArrayList from textArray again?再次从 textArray 创建 ArrayList 的方法是什么? Because I add the textArray to SQLite since SQLite does not accept an Array, but in this way I can add the items to database, but don't know how to make an array again when I retrieve them from SQLite.因为 SQLite 不接受数组,所以我将 textArray 添加到 SQLite,但是通过这种方式我可以将项目添加到数据库,但是当我从 SQLite 检索它们时不知道如何再次创建数组。

If you want to split the String at the line breaks, you can try this:如果你想在换行符处拆分String ,你可以试试这个:

String[] arraySplit = textArray.split("\n");
ArrayList<String> newList = new ArrayList<>(Arrays.asList(arraySplit));

Edit编辑

Be aware of the comment from @Robert, since this will split the given String on every line break char.请注意@Robert 的评论,因为这会在每个换行符上拆分给定的String

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM