Removing an Item from a Specific Vector Index in Java
In this tutorial, we'll see how to remove an element from Vector using its index with the remove(int index) of the Vector.public E remove(int index): Removes an element in a specific position from the Vector list and shifts all other elements to the left and decrements their indices. This method should return the object that was removed from Vector.
import java.util.Vector;Output:
public class RemoveIndexVector {
public static void main(String[] args) {
Vector< String> vct = new Vector< String> ();
//add elements
vct.add("e1");
vct.add("e2");
vct.add("e3");
vct.add("e4");
vct.add("e5");
System.out.println("Before deletion:");
for(String e:vct)
System.out.println(e);
//delete the item at the index 0
vct.remove(0);
//remove the element with the index 2
vct.remove(2);
System.out.println("\nAfter deletion:");
for(String e:vct)
System.out.println(e);
}
}
Before deletion:
e1
e2
e3
e4
e5
After deletion:
e2
e3
e5