An effective alternative is this approach. Observe the codes below:
import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class JavaSplitStringIntoArrayListEx { public static void main(String[] args) { String theStr = "A,B,C"; String[] theArr = theStr.split(","); List<String> theList = new ArrayList<String>(Arrays.asList(theArr)); theList.add("D"); System.out.println(theList); } }Notice what we did there, we passed the item to the constructor of the ArrayList using the Arrays.asList(), and we added one item to the variable theList, resulting to a normal mutable object. Check out the output below:
[A, B, C, D]
import java.util.Arrays; import java.util.List; public class JavaSplitStringIntoArrayListEx { public static void main(String[] args) { String theStr = "X,Y,Z"; String[] theArr = theStr.split(","); List<String> theList = Arrays.asList(theArr); System.out.println("The output of the code is: " + theList); } }The result should give you a fixed-size List backed by the array.
The output of the code is: [X, Y, Z]
import java.util.ArrayList; import java.util.List; public class JavaSplitStringIntoArrayListEx { public static void main(String[] args) { String theStr = "W,X,Y,Z"; String[] theArr = theStr.split(","); List<String> theList = new ArrayList<String>(); for (String item : theArr) { theList.add(item); } System.out.println(theList); } }What the program does is it manually loops the array list and append theArr into the ArrayList. This is also one way of doing Java Split String Into ArrayList.