Skip to content

WebDevHubs

  • Home
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • Java
  • Selenium
  • PHP
  • Python
  • Programs
  • Toggle search form

Java ArrayList remove() Method

Posted on July 23, 2025July 23, 2025 By Admin No Comments on Java ArrayList remove() Method

The remove() method is one of the most essential operations in the Java ArrayList class. It enables you to delete elements from a list, either by specifying the element’s index or by the element’s value directly. This makes the method a powerful tool for managing dynamic, changing collections in Java.

What Is the remove() Method in ArrayList?

  • The remove() method is used to remove elements from an ArrayList.
  • There are two overloaded versions:
    • remove(int index): Removes the element at the given position.
    • remove(Object o): Removes the first occurrence of the specified object from the list.

Syntax

// Remove by index
public E remove(int index)

// Remove by object
public boolean remove(Object o)

Parameters

ParameterDescription
index(int) The index of the element to remove. Must be between 0 and size()-1.
o(Object) The element to remove (removes first occurrence only).

Return Value

Method VersionReturn
remove(int index)Returns the element that was removed.
remove(Object o)Returns true if an element was removed, false if not found.

Exceptions

  • remove(int index):
    • Throws IndexOutOfBoundsException if the specified index is out of range.
  • remove(Object o):
    • Does not throw exceptions if the element is not found; simply returns false.
  • For custom, immutable collection implementations, may throw UnsupportedOperationException (rare for standard ArrayList).

How Does remove() Work Internally?

  • For remove(int index): The element at the specified index is removed. All subsequent elements are shifted one position to the left to fill the gap.
  • For remove(Object o): The list scans for the first occurrence using .equals(). If found, it is removed (with shifting as above). If not found, nothing changes.

Examples of the remove() Method

1. Remove by Index

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        ArrayList<String> names = new ArrayList<>();
        names.add("Alice");
        names.add("Bob");
        names.add("Charlie");

        String removed = names.remove(1); // Removes "Bob"

        // Output: Removed: Bob
        System.out.println("Removed: " + removed);
 
        // Output: [Alice, Charlie]
        System.out.println(names);     
    }
}

2. Remove by Object

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);

        // Removes 20 by value
        boolean result = numbers.remove(Integer.valueOf(20));

        // Output: Was 20 removed? true
        System.out.println("Was 20 removed? " + result);

        // Output: [10, 30]
        System.out.println(numbers);
    }
}

3. Remove Non-Existing Element

import java.util.ArrayList;

public class Demo {
    public static void main(String[] args) {
        ArrayList<String> pets = new ArrayList<>();
        pets.add("Dog");
        pets.add("Cat");

        // "Rabbit" is not in list
        boolean result = pets.remove("Rabbit");

        // Output: Was Rabbit removed? false
        System.out.println("Was Rabbit removed? " + result);

        // Output: [Dog, Cat]
        System.out.println(pets);
    }
}

4. Handling IndexOutOfBoundsException

import java.util.ArrayList;

public class ErrorExample {
    public static void main(String[] args) {
        ArrayList<String> items = new ArrayList<>();
        items.add("Pen");
        try {
            items.remove(5); // Invalid index
        } catch (IndexOutOfBoundsException e) {

            // Output: Error: Index 5 out of bounds for length 1
            System.out.println("Error: " + e.getMessage());
        }
    }
}

5. Remove Null Element

import java.util.ArrayList;

public class NullDemo {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add(null);
        list.add("Hello");

        boolean removed = list.remove(null); // Removes first null
        System.out.println("Null removed? " + removed); // Output: Null removed? true
        System.out.println(list);                       // Output: [Hello]
    }
}

Important Notes

  • First Occurrence Only: If the element exists multiple times, remove(Object o) deletes only the first one.
  • By index vs by value: For numeric lists, be careful to use remove(Integer.valueOf(n)) (object) if you mean the value, or remove(n) for an index.
  • Shifting: Elements after the removed one (by index) are shifted; index values for other elements change.
  • Return Values: Removing by index returns the removed element; by object returns true or false.
  • Null Support: You can remove null values from the list as well.
  • Performance: Both remove() variants have O(n) time complexity since they require shifting elements after removal.

Summary

AspectDetails
Method PurposeRemove element by index or value from an ArrayList
Syntaxlist.remove(int index) or list.remove(Object o)
Return ValueElement removed (by index) or true/false (by object)
ExceptionsIndexOutOfBoundsException for invalid index, none for missing obj
Element ShiftYes, after removal by index or value
Typical Use CasesDeleting entries, cleaning lists, dynamic collections

Typical Use Cases

  • Deleting user-selected items from UI lists.
  • Removing processed or invalid data dynamically.
  • Managing lists that change in size during runtime.
  • Batch cleaning or updating data collections.
Java Tags:Java-ArrayList, Java-ArrayList-Method, Java-Collections

Post navigation

Previous Post: Java ArrayList listIterator() Method
Next Post: Java ArrayList removeAll() Method

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Java ArrayList
  • Java ArrayList Methods
  • Java ArrayList add() Method
  • Java ArrayList addAll() Method
  • Java ArrayList clear() Method
  • Java ArrayList clone() Method
  • Java ArrayList contains() Method
  • Java ArrayList ensureCapacity() Method
  • Java ArrayList forEach() Method
  • Java ArrayList get() Method
  • Java ArrayList indexOf() Method
  • Java ArrayList isEmpty() Method
  • Java ArrayList iterator() Method
  • Java ArrayList lastIndexOf() Method
  • Java ArrayList listIterator() Method
  • Java ArrayList remove() Method
  • Java ArrayList removeAll() Method
  • Java ArrayList removeIf() Method
  • Java ArrayList replaceAll() Method
  • Java ArrayList retainAll() Method
  • Java ArrayList set() Method
  • Java ArrayList size() Method
  • Java ArrayList sort() Method
  • Java ArrayList spliterator() Method
  • Java ArrayList subList() Method
  • Java ArrayList toArray() Method
  • Java ArrayList trimToSize() Method

Recent Posts

  • Java ArrayList trimToSize() Method
  • Java ArrayList toArray() Method
  • Java ArrayList subList() Method
  • Java ArrayList spliterator() Method
  • Java ArrayList sort() Method

Recent Comments

No comments to show.

Important Pages

  • About Us
  • Contact Us
  • Terms of Use
  • Privacy Policy

Web Development

  • HTML
  • CSS
  • JavaScript
  • PHP

Programming Languages

  • Java
  • Python
  • PHP
  • Programs

Others

  • Selenium
  • Lodash
  • Java ArrayList
  • JavaScript Array Methods

Copyright © 2025 WebDevHubs.

Powered by PressBook Green WordPress theme