What is the Vector.removeElementAt() method in Java?

The Vector class is a growable array of objects. The elements of Vector can be accessed using an integer index and the size of a Vector can be increased or decreased.

You can read more about the Vector class here.

The removeElementAt() method of the Vector class can be used to remove the element at a specific index of the vector object. The elements after the index will be shifted one index downward. The size of the vector also decreases by 1.

Syntax

public void removeElementAt(int index)

Arguments

The only argument needed is the index of the element to be removed. The index should be positive and less than the size of the vector object. Otherwise, ArrayIndexOutOfBoundsException will be thrown.

index >= 0 && index < size()

This method doesn’t return any value.

Code

import java.util.Vector;
class RemoveElementAt {
public static void main( String args[] ) {
// Creating Vector
Vector<Integer> vector = new Vector<>();
// add elememts
vector.add(1);
vector.add(2);
vector.add(3);
System.out.println("The Vector is: " + vector);
vector.removeElementAt(1);
System.out.println("\nAfter removing element at 1st index. The Vector is: " + vector);
}
}

Please click the Run button on the code section above to see how the removeElementAt method works.

Explanation

In the code above:

  • In line 5, we created a vector object.

  • In lines 8 to 10, we added three elements (1,2,3) to the created vector object.

  • In line 14, we used the removeElementAt method of the vector object to remove the element present at the first index.