ArrayList: Few More Operations
Let's discuss some operations that can be done in an ArrayList.
Removing an element from an ArrayList
Elements can be removed from an ArrayList in the following ways.
Removing an element at a particular index
We can use the remove(int index)
method to remove an element at a particular index. The index should be less than the size of ArrayList, otherwise, IndexOutOfBoundsException
is thrown.
Removing a particular element from the ArrayList
We can also specify the element that we want to remove using the remove(Object o)
method, and the first occurrence of that element will be removed.
Removing all the elements within a range
Let’s suppose we need to remove all the elements from ...