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 anArrayList
. - 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
Parameter | Description |
---|---|
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 Version | Return |
---|---|
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.
- Throws
remove(Object o)
:- Does not throw exceptions if the element is not found; simply returns
false
.
- Does not throw exceptions if the element is not found; simply returns
- For custom, immutable collection implementations, may throw
UnsupportedOperationException
(rare for standardArrayList
).
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, orremove(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
orfalse
. - 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
Aspect | Details |
---|---|
Method Purpose | Remove element by index or value from an ArrayList |
Syntax | list.remove(int index) or list.remove(Object o) |
Return Value | Element removed (by index) or true /false (by object) |
Exceptions | IndexOutOfBoundsException for invalid index, none for missing obj |
Element Shift | Yes, after removal by index or value |
Typical Use Cases | Deleting 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.