The remove(int index) method present in java.util.ArrayList class removes the element at the specified position in this list and shifts any subsequent elements to the left (i.e. subtracts one from their indices).
public removed_element remove(int index)
Parameters: The index of the element to be removed.
Return Type: This method returns the element that was removed from the list.
Exception: This method throws IndexOutOfBoundsException if the index is out of range.
Size of list: 5 Flower ArrayList = [red-rose, tulip, sun-flower, marie-gold, orchid] Removing element at index = 2 After removing element Size of list: 4 Flower ArrayList = [red-rose, tulip, marie-gold, orchid]
When we will try to remove an element at the index which is greater than or equal to the size of the array list, the editor will give us the IndexOutOfBound Exception on running.
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 4 out of bounds for length 4
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.