Delete an Element at a Specific Index in Java ArrayList

To delete an element at a specific index in a Java ArrayList, call remove(int index). The method removes the element at that zero-based index, shifts the later elements one position to the left, and returns the removed element.

</>
Copy
E removedElement = arrayList.remove(index);

For example, index 0 refers to the first element, index 1 refers to the second element, and index 2 refers to the third element.

More about ArrayList.remove() method with Examples.

Example: Remove the Element at Index 2 from an ArrayList

In the following example, we will use remove() method to delete the second element of the ArrayList. N is passed as argument to remove() method.

DeleteElement.java

</>
Copy
import java.util.ArrayList;

public class Example {
  public static void main(String[] args) {
	  ArrayList<String> arrayList = new ArrayList<String>();  
	  arrayList.add("a");  
	  arrayList.add("b");  
	  arrayList.add("c"); 
	  arrayList.add("d");
	  arrayList.add("e");
	  System.out.println("Original ArrayList  : " + arrayList);
	  
	  int index = 2;
	  arrayList.remove(index);
	  System.out.println("Resulting ArrayList : " + arrayList);
  }
}

Output

Original ArrayList  : [a, b, c, d, e]
Resulting ArrayList : [a, b, d, e]

The value c is removed because it is stored at index 2. Although it is the third element in the list, its index is 2 because Java collections use zero-based indexing.

Get the Value Removed from the ArrayList

The remove(int index) method returns the element that was deleted. Store the return value when the program needs to process, display, or verify the removed element.

</>
Copy
import java.util.ArrayList;

public class Example {
    public static void main(String[] args) {
        ArrayList<String> colors = new ArrayList<>();
        colors.add("Red");
        colors.add("Green");
        colors.add("Blue");

        String removedColor = colors.remove(1);

        System.out.println("Removed element: " + removedColor);
        System.out.println("Updated ArrayList: " + colors);
    }
}

Output

Removed element: Green
Updated ArrayList: [Red, Blue]

Validate the ArrayList Index Before Removing an Element

A valid index must be greater than or equal to 0 and less than arrayList.size(). Calling remove() with a negative index or an index equal to or greater than the list size throws IndexOutOfBoundsException.

</>
Copy
int index = 2;

if (index >= 0 && index < arrayList.size()) {
    String removedElement = arrayList.remove(index);
    System.out.println("Removed: " + removedElement);
} else {
    System.out.println("Invalid ArrayList index: " + index);
}

Checking the bounds before removal is generally clearer than using exception handling for an index that can be validated in advance.

Note: If the index provided to the remove() function exceeds the size of the ArrayList, java.lang.IndexOutOfBoundsException occurs. To prevent this, use Java Try Catch to handle IndexOutOfBoundsException.

Difference Between remove(index) and remove(value)

ArrayList provides two commonly used remove() overloads:

  • remove(int index) removes the element at the specified position and returns that element.
  • remove(Object value) removes the first matching value and returns true if an element was removed.

This distinction is especially important for an ArrayList<Integer>. Passing an int calls the index-based overload, while passing an Integer object calls the value-based overload.

</>
Copy
import java.util.ArrayList;

public class Example {
    public static void main(String[] args) {
        ArrayList<Integer> numbers = new ArrayList<>();
        numbers.add(10);
        numbers.add(20);
        numbers.add(30);

        numbers.remove(1);                  // Removes the element at index 1: 20
        numbers.remove(Integer.valueOf(30)); // Removes the value 30

        System.out.println(numbers);
    }
}

Output

[10]

Remove the First or Last Element by Index

Use index 0 to remove the first element. To remove the last element, use size() - 1 after confirming that the list is not empty.

</>
Copy
if (!arrayList.isEmpty()) {
    Object firstElement = arrayList.remove(0);
}

if (!arrayList.isEmpty()) {
    Object lastElement = arrayList.remove(arrayList.size() - 1);
}

Remove Multiple ArrayList Elements by Index Safely

Each index after the removed element changes because the remaining elements shift to the left. When removing several known indexes, process them from the highest index to the lowest index.

</>
Copy
ArrayList<String> letters = new ArrayList<>();
letters.add("a");
letters.add("b");
letters.add("c");
letters.add("d");
letters.add("e");

int[] indexes = {3, 1};

for (int index : indexes) {
    if (index >= 0 && index < letters.size()) {
        letters.remove(index);
    }
}

System.out.println(letters);

Output

[a, c, e]

In this example, index 3 is removed before index 1. If the lower index were removed first, the positions of the later elements would change.

Removing ArrayList Elements While Iterating

Do not call arrayList.remove() directly inside an enhanced for loop. Structural changes made that way can cause ConcurrentModificationException. Use an Iterator when removal depends on each element’s value.

</>
Copy
import java.util.ArrayList;
import java.util.Iterator;

public class Example {
    public static void main(String[] args) {
        ArrayList<String> names = new ArrayList<>();
        names.add("Amy");
        names.add("Bob");
        names.add("Ann");

        Iterator<String> iterator = names.iterator();

        while (iterator.hasNext()) {
            String name = iterator.next();
            if (name.startsWith("A")) {
                iterator.remove();
            }
        }

        System.out.println(names);
    }
}

Output

[Bob]

Time Complexity of ArrayList Removal by Index

Removing the final element of an ArrayList usually requires no shifting and is constant time. Removing an element near the beginning or middle requires later elements to be shifted left, so the operation can take linear time relative to the number of shifted elements.

When an application frequently inserts or removes elements near the beginning of a collection, consider whether another data structure better matches that access pattern. An ArrayList remains suitable when indexed access is common and removals from the middle are occasional.

Common Errors When Deleting an ArrayList Element by Index

  • Using one-based positions: The third element is at index 2, not index 3.
  • Using size() as an index: The last valid index is size() - 1.
  • Ignoring an empty list: Calling remove(0) on an empty list throws an exception.
  • Confusing index and value removal: This is common with ArrayList<Integer>.
  • Removing low indexes first: During multiple removals, this changes the positions of later elements.

Java ArrayList Index Removal FAQs

How do I remove an element at a specific index in Java?

Call arrayList.remove(index), where index is between 0 and arrayList.size() - 1.

What does ArrayList.remove(index) return?

It returns the element that was removed from the specified index.

What happens after an ArrayList element is removed?

Elements located after the removed position shift one index to the left, and the list size decreases by one.

Why does remove(1) delete a value instead of the number 1?

For an ArrayList<Integer>, the primitive argument 1 selects remove(int index). To remove the value 1, use remove(Integer.valueOf(1)).

Can I remove an element from an ArrayList while iterating?

Yes. Use the iterator’s remove() method after calling next(). Avoid directly modifying the list inside an enhanced for loop.

Editorial QA Checklist for ArrayList Index Removal

  • Confirm that every example treats ArrayList indexes as zero-based.
  • Verify that valid-index checks use index >= 0 && index < arrayList.size().
  • Check that ArrayList<Integer> examples distinguish removal by index from removal by value.
  • Ensure multiple indexes are removed from highest to lowest when positions must remain stable.
  • Use Iterator.remove() for examples that remove matching elements during iteration.

Summary: Delete an ArrayList Element at Index N

Use ArrayList.remove(int index) to delete the element stored at a specific zero-based index. The method returns the removed element and shifts subsequent elements to the left. Validate that the index is within the list bounds, and take care with the overloaded remove() methods when working with integer values.

In this Java Tutorial, we learned how to delete an element present at index N of a given ArrayList.