Removing Element from Specified Index in Java Vector
Element at the specified index in Java vector can be removed using remove() method. this method is present in java.util.Vector class. We can pass index of the element which will be removed as an argument. remove() method return method returns the element that was removed.
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.
Values in vector: [10, 20, 30, 20, 40] Removed element: 30 Values in vector: [10, 20, 20, 40]
Exception in remove() method:
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