English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java Flow Control

Java Array

Java Object-Oriented (I)

Java Object-Oriented (II)

Java Object-Oriented (III)

Java Exception Handling

Java List

Java Queue (queue)

Java Map collection

Java Set collection

Java Input/Output (I/O)

Java Reader/Writer

Java other topics

Java ArrayList remove() usage and example

Java ArrayList Methods

The Java ArrayList remove() method removes a single element from the arraylist.

The syntax of the remove() method is:

//Removes the specified element
arraylist.remove(Object obj)
//Removes the element at the specified index
arraylist.remove(int index)

remove() parameters

  • obj - Element to be removed from the arraylist

  • index - Position from which the element is removed

If the same element obj exists in multiple positions, the element that first appears in the arraylist will be deleted

remove() return value

  •  If the specified element exists in the arraylist, it returns true

  •  If index is passed as a parameter, it returns the removed element

Note: If the specified index is out of range, this method will throw an IndexOutOfBoundsException.

Example1: Removes the specified element from the ArrayList

import java.util.ArrayList;
class Main {
    public static void main(String[] args) {
        //Create ArrayList
        ArrayList<String> languages = new ArrayList<>();
        // Insert element into arraylist
        languages.add("JavaScript");
        languages.add("Java");
        languages.add("Python");
        System.out.println("ArrayList: " + languages);
        //Remove element Java
        boolean result = languages.remove("Java");
        System.out.println("Has the element Java been removed? ") + result);
        System.out.println("remove() after ArrayList: " + languages);
    }
}

Output result

ArrayList: [JavaScript, Java, Python]
Has the element Java been removed? true
ArrayList after remove(): [JavaScript, Python]

In the above example, we created an ArrayList named languages. The arraylist stores the names of programming languages.

Here, we have used the remove() method in Java to delete elements from the ArrayList.

Example2:Delete element from specified position

import java.util.ArrayList;
class Main {
    public static void main(String[] args) {
        //Create ArrayList
        ArrayList<String> languages = new ArrayList<>();
        // Insert element into arraylist
        languages.add("JavaScript");
        languages.add("Java");
        languages.add("Python");
        System.out.println("ArrayList: " + languages);
        //position2Delete element from
        String element = languages.remove(2);
        System.out.println("remove() after ArrayList: " + languages);
        System.out.println("Element to be deleted: " + element);
    }
}

Output result

ArrayList: [JavaScript, Java, Python]
ArrayList after remove(): [JavaScript, Java]
Element to be deleted: Python

In the above example, we created an ArrayList named languages. Note the expression

languages.remove(2)

Here, remove() returns and deletes the position2That exists in the (i.e., Python) language.

Example3:Delete the first occurrence of an element

import java.util.ArrayList;
class Main {
    public static void main(String[] args) {
        // Create ArrayList
        ArrayList<Integer> randomNumbers = new ArrayList<>();
        // Add elements to arraylist
        randomNumbers.add(22);
        randomNumbers.add(13);
        randomNumbers.add(35);
        randomNumbers.add(13);
        randomNumbers.add(40);
        System.out.println("ArrayList: " + randomNumbers);
        //Delete the first occurrence of13
        boolean result = randomNumbers.remove(Integer.valueOf(13));
        System.out.println("The first occurrence of the element"13Was it removed? " + result);
        System.out.println("remove() after ArrayList: " + randomNumbers);
    }
}

Output result

ArrayList: [22, 13, 35, 13, 40]
The first occurrence of the element13Was it deleted? true
The ArrayList after remove(): [22, 35, 13, 40]

In the above example, we created an ArrayList named randomNumbers. In the ArrayList, there are two positions with elements13. Note this line,

randomNumbers.remove(Integer.valueOf(13))

Here,

  • Integer.valueOf() - Convert int value13Convert to Integer object. This is because the remove() method only takes object as its parameter. For more information, please visitJava Basic Types of Wrapper Objects.

  • remove() - Delete the first element that appears in the ArrayList13.

NoteWe can also use the clear() method to delete all elements from the ArrayList. For more information, please visitJava ArrayList clear().

Java ArrayList Methods