Home > Blockchain >  How to add items back to an ArrayList after appending to StringBuilder?
How to add items back to an ArrayList after appending to StringBuilder?

Time:06-16

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

CodePudding user response:

If you want to split the String at the line breaks, you can try this:

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.

  • Related