Joining ArrayLists with addAll()
To join two ArrayLists using the addAll()
method, follow the code below:
import java.util.ArrayList;
import java.util.Arrays;
public class Main {
public static void main(String[] args){
ArrayList<Integer> arrList1 = new ArrayList<>();
arrList1.add(1030);
arrList1.add(1020);
arrList1.add(1010);
arrList1.add(1040);
System.out.println(arrList1);
ArrayList<Integer> arrList2 = new ArrayList<>(Arrays.asList(1050,1060,1070));
System.out.println(arrList2);
// Joining two ArrayList
arrList1.addAll(arrList2);
System.out.println("After Joining:");
System.out.println(arrList1);
}
}
To run this code, type the following command in the terminal:
javac Main.java && java Main
This will output the following:
[1030, 1020, 1010, 1040]
[1050, 1060, 1070]
After Joining:
[1030, 1020, 1010, 1040, 1050, 1060, 1070]