Java ArrayList remove() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ArrayList

Definition

In Java, remove() deletes one element from an ArrayList. It has two overloads: remove(int index) removes the element at a position and returns it; remove(Object o) removes the first element equal to o and returns true if it found one.

After a removal, every element to the right moves one position left to close the gap, and the size drops by one.

An instance method of java.util.ArrayList (Java 1.2+): a resizable-array list with index access that allows null and duplicates and is not thread-safe.

Syntax

Remove by position, or by value.

Java
List<String> list = new ArrayList<>(Arrays.asList("a", "b", "c", "b"));
String gone = list.remove(0);        // "a"   -> [b, c, b]
boolean found = list.remove("b");    // true  -> [c, b]  (first "b" only)
boolean missing = list.remove("z");  // false -> unchanged
CallWhat it does
list.remove(i)Removes the element at index i; returns it.
list.remove(o)Removes the first element equal to o; returns true or false.
list.remove(Integer.valueOf(5))Removes the value 5 from a List<Integer>.
list.removeIf(x -> ...)Removes every element that matches a condition.

With a List<Integer>, list.remove(1) picks remove(int) and removes index 1, not the value 1, because Java prefers the overload that needs no boxing. To remove a value, pass an Integer object.

Library declarations
Java
// java.util.ArrayList<E>
public E remove(int index)
public boolean remove(Object o)

// Declared in List<E> and Collection<E>

Parameters

ParameterDescription
index (int)Only in remove(int). Position to remove, from 0 to size() - 1.
o (Object)Only in remove(Object). The value to find with equals(); may be null.

Return Value

Return ValueDescription
E (from remove(int))The element that was removed.
boolean (from remove(Object))true if a matching element was found and removed; otherwise false.

Compare with Similar Methods

Other ways to take elements out:

MethodHow it differs
removeIf(filter)Removes all matching elements, not just the first.
removeAll(c)Removes every element that is contained in c.
removeFirst()Java 21+: removes index 0; removeLast() removes the end.
clear()Removes everything at once.

Java Examples

Example 1 — Remove by Index and by Value

Remove one pet by position, one by name, and try a name that is not there.

Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ArrayListRemoveDemo {
    public static void main(String[] args) {
        List<String> pets = new ArrayList<>(Arrays.asList("cat", "dog", "fish", "dog"));

        String removed = pets.remove(2);
        System.out.println("removed " + removed + " -> " + pets);

        boolean found = pets.remove("dog");
        System.out.println("removed dog? " + found + " -> " + pets);

        System.out.println("removed bird? " + pets.remove("bird") + " -> " + pets);
    }
}

How It Works

  1. By position. remove(2) deletes the element at index 2, "fish", and returns it.
  2. Gap closed. The last "dog" slides from index 3 to index 2, so the list becomes [cat, dog, dog].
  3. By value. remove("dog") deletes only the first matching element and returns true; one "dog" is still there.
  4. Not found. "bird" is not in the list, so remove returns false and nothing changes.

Example 2 — The remove(int) vs remove(Object) Trap

With a List<Integer>, a plain number is treated as an index.

Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ArrayListRemoveIntegerTrap {
    public static void main(String[] args) {
        List<Integer> nums = new ArrayList<>(Arrays.asList(10, 20, 30, 1));

        nums.remove(1);
        System.out.println("remove(1): " + nums);

        nums.remove(Integer.valueOf(1));
        System.out.println("remove(Integer.valueOf(1)): " + nums);

        try {
            nums.remove(30);
        } catch (IndexOutOfBoundsException e) {
            System.out.println("remove(30) means index 30, not value 30");
        }
    }
}

How It Works

  1. The int argument wins. nums.remove(1) matches remove(int) exactly, so Java removes the element at index 1, which is 20, not the value 1.
  2. Force the value version. Integer.valueOf(1) is an object, so remove(Object) runs and deletes the value 1 at the end.
  3. Same trap again. remove(30) is also read as an index; with only two elements left, it throws IndexOutOfBoundsException.
  4. Safer spelling. (Integer) 30 or Integer.valueOf(30) would have removed the value 30 instead.

Example 3 — Remove While Looping: Forward vs Backward

Removing even numbers with a forward index loop skips elements; looping backwards does not.

Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ArrayListRemoveInLoop {
    public static void main(String[] args) {
        List<Integer> nums = new ArrayList<>(Arrays.asList(4, 7, 8, 8, 3));

        for (int i = 0; i < nums.size(); i++) {
            if (nums.get(i) % 2 == 0) {
                nums.remove(i);
            }
        }
        System.out.println("forward loop: " + nums);

        nums = new ArrayList<>(Arrays.asList(4, 7, 8, 8, 3));
        for (int i = nums.size() - 1; i >= 0; i--) {
            if (nums.get(i) % 2 == 0) {
                nums.remove(i);
            }
        }
        System.out.println("backward loop: " + nums);
    }
}

How It Works

  1. The skip bug. Going forward, removing index i shifts the next element into slot i, but i++ then moves past it without checking it.
  2. A missed 8. When the first 8 (at index 1) is removed, the second 8 slides into index 1, a slot already visited, so it survives.
  3. Walk backwards. Starting at the last index, a removal only shifts elements that were already checked, so nothing is skipped.
  4. Correct result. Only the odd numbers 7 and 3 remain. removeIf(n -> n % 2 == 0) would do the same in one call.

Frequently Asked Questions

remove(int index) returns the removed element. remove(Object o) returns true if an element was removed and false if no match was found.
Pass an object: list.remove(Integer.valueOf(5)) or list.remove((Integer) 5). A plain int like list.remove(5) is treated as an index.
No, only the first one. To remove every copy, use removeIf(x -> x.equals(value)) or removeAll(Collections.singleton(value)).
A for-each loop uses a hidden iterator, and removing through the list (not the iterator) makes it fail. Use iterator().remove(), removeIf, or an index loop that runs backwards.
Removing the last element is fast. Removing near the front shifts all later elements, so it takes longer on big lists.

Next: removeAll()

Remove every element found in another collection.

removeAll() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful