Below are the addAll() methods of ArrayList in Java:
- boolean addAll(Collection c) : This method appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection’s Iterator. The behavior of this operation is undefined if the specified collection is modified while the operation is in progress(implies that the behavior of this call is undefined if the specified collection is this list, and this list is nonempty).
Parameters: c : This is the collection containing elements to be added to this list. Exception: NullPointerException : If the specified collection is null
// Java program to illustrate// boolean addAll(Collection c)importjava.io.*;importjava.util.ArrayList;ÂÂpublicclassArrayListDemo {   Âpublicstaticvoidmain(String args[])   Â{       Â// create an empty array list1 with initial       Â// capacity as 5       ÂArrayList<Integer> arrlist1 =                          ÂnewArrayList<Integer>(5);       Â// use add() method to add elements in the list       Âarrlist1.add(12);       Âarrlist1.add(20);       Âarrlist1.add(45);       Â// prints all the elements available in list1       ÂSystem.out.println("Printing list1:");       Âfor(Integer number : arrlist1)           ÂSystem.out.println("Number = "+ number);       Â       Â// create an empty array list2 with an initial       Â// capacity       ÂArrayList<Integer> arrlist2 =                            ÂnewArrayList<Integer>(5);       Â// use add() method to add elements in list2       Âarrlist2.add(25);       Âarrlist2.add(30);       Âarrlist2.add(31);       Âarrlist2.add(35);       Â// let us print all the elements available in       Â// list2       ÂSystem.out.println("Printing list2:");       Âfor(Integer number : arrlist2)           ÂSystem.out.println("Number = "+ number);       Â       Â// inserting all elements, list2 will get printed       Â// after list1       Âarrlist1.addAll(arrlist2);       ÂSystem.out.println("Printing all the elements");       Â// let us print all the elements available in       Â// list1       Âfor(Integer number : arrlist1)           ÂSystem.out.println("Number = "+ number);       Â   Â}}Output:Printing list1: Number = 12 Number = 20 Number = 45 Printing list2: Number = 25 Number = 30 Number = 31 Number = 35 Printing all the elements Number = 12 Number = 20 Number = 45 Number = 25 Number = 30 Number = 31 Number = 35
boolean addAll(int index, Collection c):This method inserts all of the elements in the specified collection into this list, starting at the specified position. It shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in the list in the order that they are returned by the specified collection’s iterator.
Parameters: index : The index at which to insert the first element from the specified collection. c : This is the collection containing elements to be added to this list. Exception: IndexOutOfBoundsException : If the index is out of range NullPointerException : If the specified collection is null
// Java program to illustrate// boolean addAll(int index, Collection c)importjava.io.*;importjava.util.ArrayList;ÂÂpublicclassArrayListDemo {   Âpublicstaticvoidmain(String args[])   Â{       Â// create an empty array list1 with initial       Â// capacity 5       ÂArrayList<Integer> arrlist =                       ÂnewArrayList<Integer>(5);       Â// using add() method to add elements in the       Â// list       Âarrlist.add(12);       Âarrlist.add(20);       Âarrlist.add(45);       Â// prints all the elements available in list1       ÂSystem.out.println("Printing list1:");       Âfor(Integer number : arrlist)           ÂSystem.out.println("Number = "+ number);       Â       Â// create an empty array list2 with an initial       Â// capacity       ÂArrayList<Integer> arrlist2 =                           ÂnewArrayList<Integer>(5);       Â// use add() method to add elements in list2       Âarrlist2.add(25);       Âarrlist2.add(30);       Âarrlist2.add(31);       Âarrlist2.add(35);       Â// prints all the elements available in list2       ÂSystem.out.println("Printing list2:");       Âfor(Integer number : arrlist2)           ÂSystem.out.println("Number = "+ number);       Â       Â// inserting all elements of list2 at third       Â// position       Âarrlist.addAll(2, arrlist2);       ÂSystem.out.println("Printing all the elements");       Â// prints all the elements available in list1       Âfor(Integer number : arrlist)           ÂSystem.out.println("Number = "+ number);       Â   Â}}Output:Printing list1: Number = 12 Number = 20 Number = 45 Printing list2: Number = 25 Number = 30 Number = 31 Number = 35 Printing all the elements Number = 12 Number = 20 Number = 25 Number = 30 Number = 31 Number = 35 Number = 45
This article is contributed by Shambhavi Singh. If you like Lazyroar and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the Lazyroar main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
