Java.util.ArrayList.addall() method in Java Read Courses Practice Improve Improve Improve Like Article Like Save Article Save Report issue Report 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) import java.io.*; import java.util.ArrayList; public class ArrayListDemo { public static void main(String args[]) { // create an empty array list1 with initial // capacity as 5 ArrayList<Integer> arrlist1 = new ArrayList<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 = new ArrayList<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) import java.io.*; import java.util.ArrayList; public class ArrayListDemo { public static void main(String args[]) { // create an empty array list1 with initial // capacity 5 ArrayList<Integer> arrlist = new ArrayList<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 = new ArrayList<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 Feeling lost in the vast world of Backend Development? It's time for a change! Join our Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule. What We Offer: Comprehensive Course Expert Guidance for Efficient Learning Hands-on Experience with Real-world Projects Proven Track Record with 100,000+ Successful Geeks Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore. Last Updated : 26 Nov, 2018 Like Article Save Article Previous Java.util.ArrayList.add() Method in Java Next ArrayList clear() Method in Java with Examples Share your thoughts in the comments Add Your Comment Please Login to comment...