Yandex

Java Advanced ConceptsJava Advanced Concepts3

Java ArrayList remove() method
Syntax and Examples


Introduction

The remove() method in Java's ArrayList is a versatile tool for managing your data. It allows you to eliminate elements from the list – whether you specify their position (index) or identify them by value. This tutorial will walk you through both versions of the remove() method, complete with examples so you can see how they work in action.

Syntax

public E remove(int index)
public boolean remove(Object o)

remove(int index) Method - Removing by Index

This version of remove() allows you to delete an element from the list at a specific location. Think of it like removing someone from a line – you know exactly where they are.

Parameters

Parameter Description
index The index of the element to be removed. This is a zero-based index, meaning the first element is at index 0.

Return Value

This method returns the element that was removed from the list.

Example

Removing an Element by Index

Let's say we have a list of fruits, and we want to remove the second fruit (at index 1).

import java.util.ArrayList;

public class RemoveByIndexExample {
    public static void main(String[] args) {
        ArrayList<String> fruits = new ArrayList<>(
                "apple", "banana", "orange", "grape"
        );

        System.out.println("Original list: " + fruits);

        String removedFruit = fruits.remove(1); // Remove element at index 1 (banana)

        System.out.println("List after removal: " + fruits);
        System.out.println("Removed fruit: " + removedFruit);
    }
}
Original list: [apple, banana, orange, grape]
List after removal: [apple, orange, grape]
Removed fruit: banana

Explanation: We created an ArrayList named fruits. Then, we used fruits.remove(1) to remove the element at index 1 (which was "banana"). The method returned “banana” which is stored in removedFruit variable and printed out.

remove(Object o) Method - Removing by Value

This version of remove() lets you delete an element based on its value. It's like saying, "Remove whoever has this name!"

Parameters

Parameter Description
o The element to be removed. The method searches for the first occurrence of this element in the list.

Return Value

This method returns true if an element was successfully removed; otherwise, it returns false.

Example

Removing an Element by Value

Let's remove the "orange" from our list of fruits. If the value isn't present, the method will return false.

import java.util.ArrayList;

public class RemoveByValueExample {
    public static void main(String[] args) {
        ArrayList<String> fruits = new ArrayList<>(
                "apple", "banana", "orange", "grape"
        );

        System.out.println("Original list: " + fruits);

        boolean removed = fruits.remove("orange"); // Remove the first occurrence of "orange"

        System.out.println("List after removal: " + fruits);
        System.out.println("Was element removed? " + removed);
    }
}
Original list: [apple, banana, orange, grape]
List after removal: [apple, banana, grape]
Was element removed? true

Explanation: We created an ArrayList called fruits. Then, we used fruits.remove("orange") to remove the first occurrence of “orange”. The method returned true because "orange" was found and removed.

Removing a Non-Existent Value

Let’s try removing an element that isn't in the list, like 'kiwi'.

import java.util.ArrayList;

public class RemoveNonExistentExample {
    public static void main(String[] args) {
        ArrayList<String> fruits = new ArrayList<>(
                "apple", "banana", "orange", "grape"
        );

        System.out.println("Original list: " + fruits);

        boolean removed = fruits.remove("kiwi"); // Try to remove an element that doesn't exist

        System.out.println("List after removal attempt: " + fruits);
        System.out.println("Was element removed? " + removed); // This will print 'false'
    }
}
Original list: [apple, banana, orange, grape]
List after removal attempt: [apple, banana, orange, grape]
Was element removed? false

Explanation: Because “kiwi” isn’t in the fruits list, remove("kiwi") returns false and doesn't change the list. Note that this doesn't throw an error; it just indicates no element was removed.


Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M