Close

Java Collections - ArrayList.remove() Examples

[Last Updated: Dec 10, 2025]

Java Collections Java Java API 


Class:

java.util.ArrayList

java.lang.Objectjava.lang.Objectjava.util.AbstractCollectionjava.util.AbstractCollectionjava.util.CollectionCollectionjava.util.AbstractListjava.util.AbstractListjava.util.ListListjava.util.ArrayListjava.util.ArrayListjava.util.ListListjava.util.RandomAccessRandomAccessjava.lang.CloneableCloneablejava.io.SerializableSerializableLogicBig

Methods:

public E remove (int index)

Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices).



public boolean remove (Object o)

Removes the first occurrence of the specified element from this list, if it is present. If the list does not contain the element, it is unchanged. More formally, removes the element with the lowest index i such that Objects.equals(o, get(i)) (if such an element exists). Returns true if this list contained the specified element (or equivalently, if this list changed as a result of the call).




Examples


Example of: remove(int index)

package com.logicbig.example.arraylist;

import java.util.*;

public class RemoveExample {

public static void main(String... args) {
// Remove by index and get removed element
ArrayList<String> list = new ArrayList<>(Arrays.asList("A", "B", "C", "D"));
String removed = list.remove(1);
System.out.println("Removed: " + removed);
System.out.println("Remaining list: " + list);
}
}

Output

Removed: B
Remaining list: [A, C, D]
JDK 25




Example of: remove(Object o)

package com.logicbig.example.arraylist;

import java.util.*;

public class RemoveExample2 {

public static void main(String... args) {
// Remove first occurrence of element
ArrayList<String> list = new ArrayList<>(Arrays.asList("A", "B", "C", "B", "A"));
boolean removed = list.remove("B");
System.out.println("Removed B? " + removed);
System.out.println("List after removal: " + list);
}
}

Output

Removed B? true
List after removal: [A, C, B, A]
JDK 25




See Also