October 22, 2020 I didn't understand how to use addAll, so I will summarize how to add data to List.
The add method can add a value. You can insert at the end or position of the List.
Add to the end
List<Integer> list = new ArrayList<Integer>();
//Add value 1 to the end of integer type List
list.add(1);
To determine the insertion position, specify two arguments. The first argument specifies the place to insert, and the second argument specifies the value to insert.
Determine the insertion position
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(3);
list.add(4);
//Put 2 between 1 and 3 in List
list.add(1,,2);
addAll is a method that allows you to add multiple values at once.
//Prepare two Lists and add the value of list2 to list
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
List<Integer> list2 = new ArrayList<Integer>();
list2.add(5);
list2.add(6);
list2.add(7);
list2.add(8);
list.addAll(list2);
//The result is similar to the above code without defining a List
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.addAll(Arrays.asList(5, 6, 7, 8));
Like the add method, the addAll method can specify the position to add. The processing method is the same, and the argument is specified.
Determine the insertion position
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
List<Integer> list2 = new ArrayList<Integer>();
list2.add(5);
list2.add(6);
list2.add(7);
list2.add(8);
list.addAll(1, list2);
Create a list from a Java array (addAll / asList) (https://itsakura.com/java-collections) [Basics of Java in 3 minutes] How to add data to List (add, addAll)
Recommended Posts