Adding Elements to an ArrayList in Java

This post explains how to insert elements into an ArrayList in java. Two methods are available — arraylist add() and ArrayList addAll() — and each one is overloaded. Below we look at how to java add to list both single elements and whole collections.

1. add(E e)
2. add(int index, E e)
3. add(Collection c)
4. add(int index, Collection c)

Java ArrayList add(E e)

The ArrayList add() method inserts the supplied element into the ArrayList. It appends the specified element (e) to the end of the ArrayList. The result is a boolean value: it returns true when the element is added successfully, and false otherwise.

boolean add(E e);

Where E represents the type of elements in the ArrayList.
e is the element that you want to add in the ArrayList.
return type:  Its return type is boolean.

import java.util.ArrayList;
public class ArraylistExample 
{
	public static void main(String[] args) 
        {
			ArrayList<String> listOfNames = new ArrayList<String>();
                        listOfNames.add("JAVA");
			listOfNames.add("GOAL");
			listOfNames.add("Website");
                        // Adding Same element again
			boolean isSuccessFullyAdded = listOfNames.add("GOAL");
			System.out.println("Element added again = "+ isSuccessFullyAdded);
			for(String name : listOfNames)
			{
				System.out.println("Names from list = "+name);
			}
	}
}

Output: Element added again = true
Names from list = JAVA
Names from list = GOAL
Names from list = Website
Names from list = GOAL

arraylist add() arraylist addAll()

This shows why duplicates can appear in an ArrayList. Every element is kept at its own index value, so the list never checks whether that element already exists.

add(int index, E e)

Use this overload when you need to place a given element at a given position in the ArrayList. Imagine an ArrayList holding 4 elements and you want to insert a new element at position 2. Its return type is void. When the element is inserted at that position, the element currently sitting there is shifted along.

boolean add(int index, E e);

Where index, index at which the specified element is to be inserted.
E, represents the type of elements in ArrayList.
e, is the element which you want to add in ArrayList.
return type:  Its return type is void.
throw, IndexOutOfBoundsException if index is invalid.

import java.util.ArrayList;
public class ArraylistExample 
{
	public static void main(String[] args)
        {
			ArrayList<String> listOfNames = new ArrayList<String>();
			listOfNames.add("JAVA");
			listOfNames.add("GOAL");
			listOfNames.add("Website");
                        // Adding element at specified position
	                listOfNames.add(1, "Learning");
			for(String name : listOfNames)
			{
				System.out.println("Names from list = "+name);
			}
	}
}

Output: Names from list = JAVA
Names from list = Learning
Names from list = GOAL
Names from list = Website

arraylist add() arraylist addAll()

ArrayList addAll(Collection c)

In Java, ArrayList addAll() appends every element of the specified collection to the end of the ArrayList. It returns a boolean value, returning true once the collection has been added. If the specified collection is null, it throws a NullPointerException.

boolean addAll(Collection c);

Where, c collection containing elements to be added to this list.
throws NullPointerException, if the specified collection is null.
return type,  Its return type is boolean.

import java.util.ArrayList;
public class ArrayListExample 
{
	public static void main(String[] args) 
        {
		ArrayList<String> listOfNames = new ArrayList<String>();
		listOfNames.add("JAVA");
		listOfNames.add("GOAL");
		listOfNames.add("Website");
		ArrayList<String> listOfNames2 = new ArrayList<String>();
		listOfNames2.add("Learning");
		listOfNames2.add("Concepts");
		listOfNames.addAll(listOfNames2);
		for(String name : listOfNames)
		{
			System.out.println("Names from list = "+name);
		}
	}
}

Output: Names from list = JAVA
Names from list = GOAL
Names from list = Website
Names from list = Learning
Names from list = Concepts

arraylist add() arraylist addAll()

ArrayList addAll(int index, Collection c)

The ArrayList addAll(int index, Collection c) method appends all the elements of the specified collection at a chosen position in the ArrayList. Its return type is boolean. It inserts every element of the specified collection into the ArrayList starting at that position, shifting the elements already present there.

boolean addAll(int index, Collection c);

Where index, index at which the specified element is to be inserted.
c, collection containing elements to be added to this list.
throws NullPointerException, if the specified collection is null.
return type,  it’s return type is boolean.

package package1;
import java.util.ArrayList;
public class ArrayListExample 
{
	public static void main(String[] args) 
        {
		ArrayList<String> listOfNames = new ArrayList<String>();
		listOfNames.add("JAVA");
		listOfNames.add("GOAL");
		listOfNames.add("Website");
		ArrayList<String> listOfNames2 = new ArrayList<String>();
		listOfNames2.add("Learning");
		listOfNames2.add("Concepts");
		listOfNames.addAll(1, listOfNames2);
		for(String name : listOfNames)
		{
			System.out.println("Names from list = "+name);
		}
	}
}

Output: Names from list = JAVA
Names from list = Learning
Names from list = Concepts
Names from list = GOAL
Names from list = Website

arraylist add() arraylist addAll()

Frequently Asked Questions

What is the difference between ArrayList add() and ArrayList addAll()?

add() inserts a single element into the ArrayList, while addAll() appends all the elements of a specified collection to the ArrayList. Both methods are overloaded, so each also has a variant that takes an index to insert at a specific position.

Does ArrayList add() allow duplicate elements?

Yes. As shown in the example on this page, adding the same element again returns true and the duplicate is stored at a different index, because ArrayList does not check whether an element already exists.

What does ArrayList add() return?

The add(E e) and addAll(Collection c) overloads return a boolean (true if the element or collection was added successfully). The index-based overloads add(int index, E e) and addAll(int index, Collection c) are described on this page as returning void.

What happens when I add an element at a specific index?

When you use add(int index, E e) or addAll(int index, Collection c), the new element(s) are inserted at the specified position and the elements already present at that position and after it are shifted.

Which exceptions can ArrayList add() and addAll() throw?

The index-based add(int index, E e) throws IndexOutOfBoundsException if the index is invalid, and addAll() throws NullPointerException if the specified collection is null.

Leave a Comment

Both add() and addAll() are overloaded, so you can either append to the end of the list or insert at a chosen index. Remember that the index-based variants shift the existing elements, and that addAll() needs a non-null collection. For related operations, see how to replace an element or remove an element from an ArrayList.