Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
HowToDoInJava
  • Java
  • Spring AI
  • Spring Boot
  • Hibernate
  • JUnit 5
  • Interview

Java ArrayList remove(): Remove a Single Element from List

ArrayList remove() removes the first occurrence of the specified element from this list, if it is present, else the list remains unchanged.

Lokesh Gupta

August 7, 2023

Java ArrayList
Java ArrayList
ArrayList

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

1. Syntax

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

  • boolean remove(Object o) – removes the first occurrence of the specified element by value from the list. Returns true is any element is removed from the list, or else false.
  • Object remove(int index) – 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 IndexOutOfBoundsException if the argument index is invalid.

2. Examples to remove an element from ArrayList

2.1. Removing only the First Occurrence of the Element

Java program to remove an object from an ArrayList using remove() method. In the following example, we invoke the remove() method two times.

  • If the element is found in the list, then the first occurrence of the item is removed from the list.
  • Nothing happens if the element is NOT found in the list, and the list remains unchanged.

The following list contains the string “C” twice. The remove() method removes only a single occurrence everytime.

ArrayList<String> alphabets = new ArrayList<>(Arrays.asList("A", "B", "C", "C", "D"));  

alphabets.remove("C");   //[A, B, C, D]

alphabets.remove("C");  //[A, B, D]

alphabets.remove("Z");  //[A, B, D]  - List is unchanged

2.2. Remove All Occurrences of an Element

We cannot directly remove all occurrences of any element from the 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.

ArrayList<String> alphabets = new ArrayList<>(Arrays.asList("A", "B", "C", "C", "D"));

alphabets.removeAll(Collections.singleton("C"));   //[A, B, D]

3. Remove an Element by Index

While removing an element using the index, we must be very careful about the list size and index argument. Java program to remove an object by its index position from an ArrayList using remove() method.

ArrayList<String> alphabets = new ArrayList<>(Arrays.asList("A", "B", "C", "D"));

alphabets.remove(2);  //Index in range - removes 'C'

alphabets.remove(10);    //Index out of range - IndexOutOfBoundsException

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 !!

Comments

Subscribe
Notify of
2 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

ArrayList Methods

  • ArrayList add()
  • ArrayList addAll()
  • ArrayList clone()
  • ArrayList contains()
  • ArrayList ensureCapacity()
  • ArrayList forEach()
  • ArrayList get()
  • Arraylist indexOf()
  • Arraylist lastIndexOf()
  • ArrayList listIterator()
  • ArrayList remove()
  • ArrayList removeAll()
  • ArrayList removeIf()
  • ArrayList retainAll()
  • ArrayList spliterator()
  • ArrayList subList()
  • ArrayList toArray()

ArrayList Examples

  • Initialize Arraylist
  • Iteration
  • Add/replace Element
  • Add Multiple Elements
  • Check Empty List
  • Remove Element
  • Replace Element
  • Empty ArrayList
  • Synchronized ArrayList
  • Compare two lists
  • Remove duplicates
  • Merge two lists
  • Serialize a List
  • Swap two elements
  • Convert Array to ArrayList
  • Convert HashSet to ArrayList
  • Convert LinkedList to ArrayList
  • ArrayList vs LinkedList
  • ArrayList vs Vector

Table of Contents

  • 1. Syntax
  • 2. Examples to remove an element from ArrayList
    • 2.1. Removing only the First Occurrence of the Element
    • 2.2. Remove All Occurrences of an Element
  • 3. Remove an Element by Index
Photo of author

Lokesh Gupta

A fun-loving family man, passionate about computers and problem-solving, with over 15 years of experience in Java and related technologies. An avid Sci-Fi movie enthusiast and a fan of Christopher Nolan and Quentin Tarantino.
Follow on Twitter Portfolio

Previous

Java ArrayList.listIterator()

Next

Java ArrayList removeAll(): Remove All Occurrences of Element

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Tutorial Series

OOP

Regex

Maven

Logging

TypeScript

Python

Meta Links

About Us

Advertise

Contact Us

Privacy Policy

Our Blogs

REST API Tutorial

Follow On:

  • Github
  • LinkedIn
  • Twitter
  • Facebook
Copyright © 2026 | Sitemap