ArrayList addAll() method example

ArrayList addAll() method is used to append all of the elements of argument collection to the list at the end. The order of appended elements is that they are returned by the argument collection’s Iterator.

Please note that We can add elements of any type in arraylist, but make program behave in more predicatable manner, we should add elements of one certain type only in any goven list instance. Use generics for compile time type safety while adding the element to arraylist.

1. ArrayList addAll() syntax

addAll() method first ensures that there is sufficient space in the arraylist. If list does not have space, then it grows the list by adding more spaces in underlying array. Then it append the elements to end of the list.

public boolean addAll(Collection<? extends E> c) {
    Object[] a = c.toArray();
    int numNew = a.length;
    ensureCapacityInternal(size + numNew);  // Increments modCount
    System.arraycopy(a, 0, elementData, size, numNew);
    size += numNew;
    return numNew != 0;
}
  • Method parameter – collection containing elements to be added to this list.
  • Method returns true if this list changed as a result of the call.
  • Method throws NullPointerException if the specified collection is null.

2. ArrayList addAll(Collection c) example

Java program to add all elements of a given collection to the arraylist using addAll() method. Always use generics to ensure you add only a certain type of element in a given list.

import java.util.ArrayList;

public class ArrayListExample 
{
    public static void main(String[] args) 
    {
        ArrayList<String> list1 = new ArrayList<>();    //list 1
        
        list1.add("A");
        list1.add("B");
        list1.add("C");
        list1.add("D");
        
        ArrayList<String> list2 = new ArrayList<>();    //list 2
        
        list2.add("E");
        
        list1.addAll(list2);
        
        System.out.println(list1);      //combined list
    }
}

Program output.

[A, B, C, D, E]

2. ArrayList addAll (int fromIndex, Collection c) example

This method is overloaded variant of previous method. It accepts one more argument 'fromIndex' at which to insert the first element from the specified collection.

Indices start from '0'.

import java.util.ArrayList;

public class ArrayListExample 
{
    public static void main(String[] args) 
    {
        ArrayList<String> list1 = new ArrayList<>();    //list 1
        
        list1.add("A");
        list1.add("B");
        list1.add("C");
        list1.add("D");
        
        ArrayList<String> list2 = new ArrayList<>();    //list 2
        
        list2.add("E");	
        list2.add("F");	
        
        list1.addAll(2, list2);	//Elements will be inserted from index 2 
        
        System.out.println(list1);      //combined list
    }
}

Program output.

[A, B, E, F, C, D]

Happy Learning !!

Read More:

A Guide to Java ArrayList
ArrayList Java Docs

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.