How to Remove Element from ArrayList in Java - Simple Guide
To remove an
ArrayList element in Java, use the remove() method. You can remove by index using remove(int index) or by value using remove(Object o).Syntax
The ArrayList class provides two main remove() methods:
remove(int index): Removes the element at the specified position (index) in the list.remove(Object o): Removes the first occurrence of the specified element from the list.
Indexes start at 0, so the first element is at index 0.
java
arrayList.remove(2); // removes element at index 2 arrayList.remove("apple"); // removes first "apple" element
Example
This example shows how to remove elements by index and by value from an ArrayList of strings.
java
import java.util.ArrayList; public class RemoveElementExample { public static void main(String[] args) { ArrayList<String> fruits = new ArrayList<>(); fruits.add("apple"); fruits.add("banana"); fruits.add("cherry"); fruits.add("date"); fruits.add("apple"); System.out.println("Original list: " + fruits); // Remove element at index 1 ("banana") fruits.remove(1); System.out.println("After removing index 1: " + fruits); // Remove first occurrence of "apple" fruits.remove("apple"); System.out.println("After removing first 'apple': " + fruits); } }
Output
Original list: [apple, banana, cherry, date, apple]
After removing index 1: [apple, cherry, date, apple]
After removing first 'apple': [cherry, date, apple]
Common Pitfalls
Common mistakes when removing elements from an ArrayList include:
- Using
remove(int index)when you mean to remove by value, which can cause unexpected removals if the list contains integers. - Trying to remove an element that does not exist, which returns
falsebut does not throw an error. - Modifying the list while iterating over it without using an iterator, which causes
ConcurrentModificationException.
java
import java.util.ArrayList; public class RemovePitfall { public static void main(String[] args) { ArrayList<Integer> numbers = new ArrayList<>(); numbers.add(1); numbers.add(2); numbers.add(3); // Wrong: removes element at index 2, not the number 2 numbers.remove(2); System.out.println("After remove(2): " + numbers); // Output: [1, 2] // Right: remove object 2 by casting to Integer numbers.remove(Integer.valueOf(2)); System.out.println("After remove(Integer.valueOf(2)): " + numbers); // Output: [1, 3] } }
Output
After remove(2): [1, 2]
After remove(Integer.valueOf(2)): [1, 3]
Quick Reference
Here is a quick summary of how to remove elements from an ArrayList:
| Method | Description | Example |
|---|---|---|
| remove(int index) | Removes element at the given index | list.remove(0); // removes first element |
| remove(Object o) | Removes first occurrence of the object | list.remove("apple"); // removes first "apple" |
| clear() | Removes all elements from the list | list.clear(); // list becomes empty |
Key Takeaways
Use remove(int index) to delete element by position in the list.
Use remove(Object o) to delete element by value; it removes the first match.
Be careful with remove(int) on lists of Integer to avoid confusion between index and value.
Removing elements while iterating requires using an iterator to avoid errors.
If the element to remove does not exist, remove(Object) returns false but does not throw an error.