ArrayList addAll() Method in Java

Published by user on

In this post, we will look at what ArrayList addAll() method does and how to use it. We will also look at the programs to demonstrate their usage.

If you want to add multiple elements to the ArrayList then the best option is to use the addAll() method.

What ArrayList addAll() method does?

It accepts the specified collection and adds the new elements to the ArrayList. The order of the new elements is the same as the order returned by the specified collection’s iterator.

This method has two variants, without index and with index.

The addAll() method without index adds all the elements to the end of the List. If you use the one with index then it adds the new elements at the specified index and shifts the existing elements to the right.

Syntax:

Let’s look at the syntax.

addAll(Collection c)
boolean addAll(int index, Collection c)

Parameter:
index is the position to insert the elements
c is the collection of elements to add to the ArrayList

Exception:
IndexOutOfBoundsException – If the index is out of range i.e. (index < 0 OR index > size()).
NullPointerException – If the specified collection is null.

Program 1:

addAll(Collection c): It adds the elements to the end of the list. Let’s look at the example.

import java.util.ArrayList;

// Demonstrate the use of addAll(Collection c)
public class ArrayListDemo {

	public static void main(String[] args) {
		// declare arrList1
		ArrayList<Integer> arrList1 = new ArrayList<>();
		arrList1.add(10);
		arrList1.add(20);
		arrList1.add(30);

		System.out.println("Elements in List 1:");
		for (Integer i : arrList1) {
			System.out.println("Value = " + i);
		}

		// declare arrList2
		ArrayList<Integer> arrList2 = new ArrayList<>();
		arrList2.add(40);
		arrList2.add(50);
		arrList2.add(60);
		System.out.println("Elements in List 2:");
		for (Integer i : arrList2) {
			System.out.println("Value = " + i);
		}

		// call addAll to add arrList1 to arrList2
		arrList2.addAll(arrList1);
		System.out.println("All elements after calling addAll:");
		for (Integer i : arrList2) {
			System.out.println("Value = " + i);
		}
	}
}

Output:

Elements in List 1:
Value = 10
Value = 20
Value = 30
Elements in List 2:
Value = 40
Value = 50
Value = 60
All elements after calling addAll:
Value = 40
Value = 50
Value = 60
Value = 10
Value = 20
Value = 30

Program 2:

ArrayList.addAll(int index, Collection c): It adds the given collection to the specified index.

import java.util.ArrayList;

// This program demonstrates the use of 
// addAll(int index, Collection<? extends E> c)
public class ArrayListDemo {
	public static void main(String[] args) {
		
		//create the list1
		ArrayList<Integer> arrList1 = new ArrayList<>();
		arrList1.add(10);
		arrList1.add(20);
		arrList1.add(30);
		
		System.out.println("Elements in List 1:");
		for (Integer i : arrList1) {
			System.out.println("Value = " + i);
		}

		//create the list2
		ArrayList<Integer> arrList2 = new ArrayList<>();
		arrList2.add(40);
		arrList2.add(50);
		arrList2.add(60);
		
		System.out.println("Elements in List 2:");
		for (Integer i : arrList2) {
			System.out.println("Value = " + i);
		}
		
		// call addAll(index, Collection)
		arrList2.addAll(2, arrList1);
		
		System.out.println("All elements after calling addAll:");
		for (Integer i : arrList2) {
			System.out.println("Value = " + i);
		}
	}
}

Output:

Elements in List 1:
Value = 10
Value = 20
Value = 30
Elements in List 2:
Value = 40
Value = 50
Value = 60
All elements after calling addAll:
Value = 40
Value = 50
Value = 10
Value = 20
Value = 30
Value = 60

Related Articles:

List addAll() Method in Java

Categories: Java