list.remove() / clear() / size() / isEmpty()
Methods for removing elements from a list, clearing the entire list, and checking the number of elements or whether the list is empty. These are essential basic operations for managing and inspecting list state.
Syntax
// Removes the element at the specified index. list.remove(int index); // Removes the first element equal to the specified object. list.remove(Object o); // Removes all elements from the list. list.clear(); // Returns the number of elements in the list. list.size(); // Returns whether the list is empty. list.isEmpty();
Method List
| Method | Description |
|---|---|
| remove(int index) | Removes and returns the element at the specified index. Elements after it are shifted down by one. |
| remove(Object o) | Removes the first element equal to the specified object (determined by equals()). Returns true if an element was removed. |
| clear() | Removes all elements from the list, leaving it empty. |
| size() | Returns the number of elements contained in the list. |
| isEmpty() | Returns true if the list is empty (has zero elements), or false otherwise. |
Sample Code
import java.util.ArrayList;
ArrayList<String> list = new ArrayList<>();
list.add("apple");
list.add("banana");
list.add("cherry");
list.add("banana");
System.out.println(list); // Prints "[apple, banana, cherry, banana]"
// Remove by index.
list.remove(0); // Removes "apple" at index 0.
System.out.println(list); // Prints "[banana, cherry, banana]"
// Remove by object (only the first matching element).
list.remove("banana");
System.out.println(list); // Prints "[cherry, banana]"
// Check the size with size().
System.out.println(list.size()); // Prints "2"
// Check whether the list is empty with isEmpty().
System.out.println(list.isEmpty()); // Prints "false"
// Clear the list with clear().
list.clear();
System.out.println(list); // Prints "[]"
System.out.println(list.isEmpty()); // Prints "true"
// Note the difference between remove(int) and remove(Object) with an Integer list.
ArrayList<Integer> nums = new ArrayList<>(java.util.Arrays.asList(10, 20, 30));
nums.remove(0); // Removes the element at index 0 (value 10).
System.out.println(nums); // Prints "[20, 30]"
nums.remove(Integer.valueOf(20)); // Removes the element with value 20.
System.out.println(nums); // Prints "[30]"
Notes
remove() is overloaded: when the argument is of type int, it removes by index; when it is of type Object, it removes by value. With an ArrayList<Integer>, writing remove(20) attempts to remove the element at index 20, not the value 20. To remove by value, use remove(Integer.valueOf(20)) to explicitly wrap the value.
Modifying a list while iterating over it can throw a ConcurrentModificationException. To safely remove elements inside a loop, use the Iterator's remove() method, or removeIf() available in Java 8 and later.
To add or retrieve elements, see add() / get(). To search for elements, see contains() / indexOf().
If you find any errors or copyright issues, please contact us.