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 5ArrayList<Integer> arrlist1 =newArrayList<Integer>(5);// use add() method to add elements in the listarrlist1.add(12);arrlist1.add(20);arrlist1.add(45);// prints all the elements available in list1System.out.println("Printing list1:");for(Integer number : arrlist1)System.out.println("Number = "+ number);// create an empty array list2 with an initial// capacityArrayList<Integer> arrlist2 =newArrayList<Integer>(5);// use add() method to add elements in list2arrlist2.add(25);arrlist2.add(30);arrlist2.add(31);arrlist2.add(35);// let us print all the elements available in// list2System.out.println("Printing list2:");for(Integer number : arrlist2)System.out.println("Number = "+ number);// inserting all elements, list2 will get printed// after list1arrlist1.addAll(arrlist2);System.out.println("Printing all the elements");// let us print all the elements available in// list1for(Integer number : arrlist1)System.out.println("Number = "+ number);}}chevron_rightfilter_noneOutput: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 5ArrayList<Integer> arrlist =newArrayList<Integer>(5);// using add() method to add elements in the// listarrlist.add(12);arrlist.add(20);arrlist.add(45);// prints all the elements available in list1System.out.println("Printing list1:");for(Integer number : arrlist)System.out.println("Number = "+ number);// create an empty array list2 with an initial// capacityArrayList<Integer> arrlist2 =newArrayList<Integer>(5);// use add() method to add elements in list2arrlist2.add(25);arrlist2.add(30);arrlist2.add(31);arrlist2.add(35);// prints all the elements available in list2System.out.println("Printing list2:");for(Integer number : arrlist2)System.out.println("Number = "+ number);// inserting all elements of list2 at third// positionarrlist.addAll(2, arrlist2);System.out.println("Printing all the elements");// prints all the elements available in list1for(Integer number : arrlist)System.out.println("Number = "+ number);}}chevron_rightfilter_noneOutput: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 GeeksforGeeks 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 GeeksforGeeks 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.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Java.util.Collections.rotate() Method in Java with Examples
- Java.util.Collections.disjoint() Method in java with Examples
- Java.lang.string.replace() method in Java
- Java.util.ArrayList.add() Method in Java
- Java.util.BitSet.set() method in Java
- Java 8 | ArrayDeque removeIf() method in Java with Examples
- Java lang.Long.lowestOneBit() method in Java with Examples
- Java lang.Long.numberOfTrailingZeros() method in Java with Examples
- Java lang.Long.numberOfLeadingZeros() method in Java with Examples
- Java lang.Long.highestOneBit() method in Java with Examples
- Java lang.Long.byteValue() method in Java with Examples
- Java lang.Long.reverse() method in Java with Examples
- Java lang.Long.builtcount() method in Java with Examples
- Java.math.BigInteger.modInverse() method in Java
- Java.math.BigInteger.probablePrime() method in Java
- Java Clock tickMinutes() method in Java with Examples
- Java Clock withZone() method in Java with Examples
- Java.lang.Short toString() method in Java with Examples
- Method Class | getName() Method in Java
- Method Class | toGenericString() method in Java

