Insert an Element at a Specific Index in a Java ArrayList

To insert an element at a specific position in a Java ArrayList, use the overloaded add(int index, E element) method. The index is zero-based, so index 0 represents the first position, index 1 represents the second position, and so on.

When an element is inserted, the element currently at that index and all subsequent elements are shifted one position to the right. No existing element is replaced.

ArrayList add(index, element) Syntax

</>
Copy
arrayList.add(index, element);

The parameters are:

  • index: the zero-based location where the new element must be inserted.
  • element: the value or object to insert into the list.

A valid insertion index is from 0 through arrayList.size(), inclusive. Using size() inserts the element at the end of the list.

Java Program to Insert an Element at Index 2

In the following example, Python is inserted at index 2, which is the third position in the list.

InsertElement.java

</>
Copy
import java.util.ArrayList;

public class InsertElement {

	public static void main(String[] args) {
		ArrayList<String> names = new ArrayList<String>();
		
		names.add("Java");
		names.add("Kotlin");
		names.add("Android");
		
		names.add(2,"Python");
		
		names.forEach(name -> {
			System.out.println(name);
		});
	}
}

Output

Java
Kotlin
Python
Android

Before insertion, Android is stored at index 2. Calling names.add(2, "Python") places Python at index 2 and shifts Android to index 3.

Insert an Element at the Beginning of an ArrayList

Use index 0 to insert an element at the front of an ArrayList.

</>
Copy
ArrayList<String> languages = new ArrayList<>();
languages.add("Kotlin");
languages.add("Python");

languages.add(0, "Java");

System.out.println(languages);

Output

[Java, Kotlin, Python]

Every existing element is shifted one position to the right when an item is inserted at index 0.

Insert an Element at the End Using size()

An element can be inserted at the end by passing the current list size as the index.

</>
Copy
ArrayList<String> languages = new ArrayList<>();
languages.add("Java");
languages.add("Kotlin");

languages.add(languages.size(), "Python");

System.out.println(languages);

Output

[Java, Kotlin, Python]

For normal appending, languages.add("Python") is shorter and has the same effect.

Valid and Invalid ArrayList Insertion Indexes

For a list containing n elements, an insertion index must satisfy the following condition:

</>
Copy
0 <= index <= arrayList.size()

For example, a list with three elements has a size of 3. Valid insertion indexes are therefore 0, 1, 2, and 3.

IndexInsertion result
0Insert before the first element
1 to size() - 1Insert between existing elements
size()Insert after the final element
Less than 0 or greater than size()Throw IndexOutOfBoundsException

Handling IndexOutOfBoundsException During Insertion

The add(index, element) method throws an IndexOutOfBoundsException when the supplied index is negative or greater than the current list size.

</>
Copy
ArrayList<String> languages = new ArrayList<>();
languages.add("Java");
languages.add("Kotlin");

int index = 5;
String value = "Python";

if (index >= 0 && index <= languages.size()) {
    languages.add(index, value);
} else {
    System.out.println("Invalid insertion index: " + index);
}

Output

Invalid insertion index: 5

Difference Between ArrayList add() and set()

Use add(index, element) when a new element must be inserted. Use set(index, element) when the existing element at an index must be replaced.

MethodOperationChanges list sizeEffect on later elements
add(index, element)Inserts a new elementYesShifts them to the right
set(index, element)Replaces an existing elementNoDoes not shift them
</>
Copy
ArrayList<String> languages = new ArrayList<>();
languages.add("Java");
languages.add("Kotlin");
languages.add("Python");

languages.add(1, "C++");
System.out.println(languages);

languages.set(1, "C#");
System.out.println(languages);

Output

[Java, C++, Kotlin, Python]
[Java, C#, Kotlin, Python]

ArrayList Insertion Time Complexity

Inserting an element at the beginning or middle of an ArrayList generally takes O(n) time because later elements must be shifted. Appending an element with add(element) is typically constant time on average, although an occasional internal array resize requires copying the stored elements.

When a program performs frequent insertions near the beginning of a large list, consider whether a different data structure better matches the access and insertion requirements.

Frequently Asked Questions About ArrayList Index Insertion

How do I insert an element at a specific position in an ArrayList?

Call arrayList.add(index, element). For example, names.add(2, "Python") inserts Python at index 2 and shifts the existing element at that index to the right.

Can I insert an element at index equal to ArrayList size?

Yes. An index equal to arrayList.size() is valid and inserts the element at the end. An index greater than the size is invalid.

Does add(index, element) replace the existing ArrayList element?

No. It inserts a new element and shifts the existing element and later elements to the right. Use set(index, element) to replace an existing value.

How do I add an element to the front of an ArrayList?

Pass 0 as the index, as in arrayList.add(0, element).

Why does ArrayList insertion throw IndexOutOfBoundsException?

The exception occurs when the insertion index is less than 0 or greater than arrayList.size(). Check the index before inserting when it comes from user input or another variable source.

ArrayList Insertion Editorial QA Checklist

  • Confirm that every insertion example uses a zero-based index.
  • Verify that the stated valid index range includes arrayList.size().
  • Check that insertion is not described as replacement; replacement requires set().
  • Confirm that each output reflects the rightward shift of existing elements.
  • Verify that invalid-index examples correctly identify IndexOutOfBoundsException.

Summary of Inserting an Element in ArrayList

Use ArrayList.add(index, element) to insert a new item at a specific zero-based index. Existing elements from that index onward are shifted to the right. The index must be between 0 and size(), inclusive. To replace rather than insert an element, use set(index, element).

In this Java Tutorial, we learned how to insert an element in ArrayList at specific index in Java.