1import java.util.ArrayList;
2public class ArrayListAddAllMethod
3{
4 public static void main(String[] args)
5 {
6 // creating an empty ArrayList with initial capacity
7 ArrayList<Integer> al1 = new ArrayList<Integer>(5);
8 al1.add(11);
9 al1.add(13);
10 al1.add(15);
11 // printing all elements of al1
12 System.out.println("ArrayList 1 elements: ");
13 for(Integer number : al1)
14 {
15 System.out.println("Number: " + number);
16 }
17 // creating another empty ArrayList with initial capacity
18 ArrayList<Integer> al2 = new ArrayList<Integer>(5);
19 al2.add(12);
20 al2.add(14);
21 al2.add(16);
22 al2.add(18);
23 // printing all elements of ArrayList2
24 System.out.println("ArrayList 2 elements: ");
25 for(Integer number : al2)
26 {
27 System.out.println("Number: " + number);
28 }
29 // insert all elements of al2 at third position
30 al1.addAll(2, al2);
31 System.out.println("Print all elements: ");
32 // printing all elements in al1
33 for(Integer number : al1)
34 {
35 System.out.println("Number: " + number);
36 }
37 }
38}
1import java.util.ArrayList;
2public class ArrayListAddMethodExample
3{
4 public static void main(String[] args)
5 {
6 // creating an empty ArrayList with initial capacity
7 ArrayList<Integer> al = new ArrayList<Integer>(6);
8 al.add(12);
9 al.add(14);
10 al.add(16);
11 al.add(18);
12 al.add(20);
13 // adding element 15 at fourth position
14 al.add(3, 15);
15 for(Integer number : al)
16 {
17 System.out.println("Number: " + number);
18 }
19 }
20}