ArrayList remove() method example

ArrayList remove() removes the first occurrence of the specified element from this list, if it is present. If the list does not contain the element, list remain unchanged.

1. ArrayList remove() method

The remove() method is overloaded and comes in two variants:

  • boolean remove(Object o) – removes the first occurrence of the specified element from the list. Returns true is any element was removed from the list, else false.
  • Object remove(int index) throws IndexOutOfBoundsException – removes the element at the specified position in this list. Shifts any subsequent elements to the left. Returns the removed element from the list. Throws exception if argument index is invalid.

2. ArrayList remove(Object o) example

2.1. Remove only first occurrence

Java program to remove an object from an arraylist using remove() method.

import java.util.ArrayList;
import java.util.Arrays;

public class ArrayListExample 
{
    public static void main(String[] args) throws CloneNotSupportedException 
    {
        ArrayList<String> alphabets = new ArrayList<>(Arrays.asList("A", "B", "C", "D"));
        
        System.out.println(alphabets);
        
        alphabets.remove("C");          //Element is present
        
        System.out.println(alphabets);
        
        alphabets.remove("Z");          //Element is NOT present
        
        System.out.println(alphabets);
    }
}

Program output.

[A, B, C, D]
[A, B, D]
[A, B, D]

2.2. Remove all occurrences of element

We cannot directly remove all occurrences of any element from list using remove() method. We can use removeAll() method for this purpose.

Java program to remove all the occurrences of an object from the arraylist.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

public class ArrayListExample 
{
    public static void main(String[] args) throws CloneNotSupportedException 
    {
        ArrayList<String> alphabets = new ArrayList<>(Arrays.asList("A", "B", "A", "D", "A"));
        
        System.out.println(alphabets);
        
        alphabets.removeAll(Collections.singleton("A"));
        
        System.out.println(alphabets);
    }
}

Program output.

[A, B, A, D, A]
[B, D]

3. ArrayList remove(int index) example

Java program to remove an object by its index position from an arraylist using remove() method.

import java.util.ArrayList;
import java.util.Arrays;

public class ArrayListExample 
{
    public static void main(String[] args) throws CloneNotSupportedException 
    {
        ArrayList<String> alphabets = new ArrayList<>(Arrays.asList("A", "B", "C", "D"));
        
        System.out.println(alphabets);
        
        alphabets.remove(2);          //Index in range - removes 'C'
        
        System.out.println(alphabets);
        
        alphabets.remove(10);          //Index out of range - exception
    }
}

Program output.

[A, B, C, D]
[A, B, D]
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 10, Size: 4
	at java.util.ArrayList.rangeCheck(ArrayList.java:653)
	at java.util.ArrayList.remove(ArrayList.java:492)
	at com.howtodoinjava.example.ArrayListExample.main(ArrayListExample.java:18)

That’s all for the ArrayList remove() method in Java.

Happy Learning !!

Read More:

A Guide to Java ArrayList
ArrayList Java Docs

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

6 thoughts on “ArrayList remove() method example”

  1. alphabets.remove(“2”); //Index in range – removes ‘C’

    u can’t remove an object using string index => like (“2”)
    remove “” it should be => alphabets.remove(“2”);
    or result it will be the same [A, B, C, D]

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.