Java Vector - remove() Method
The java.util.Vector.remove() method is used to remove the first occurrence of the specified element from this vector, if it is present. It shifts any subsequent elements to the left by subtracting one from their indices. Every removal of element results into reducing the vector size by one unless the vector is empty.
Syntax
public boolean remove(Object obj)
Parameters
obj |
Specify the element which need to be removed from this vector, if present. |
Return Value
Returns true if this vector contained the specified element.
Exception
NA.
Example:
In the example below, the java.util.Vector.remove() method is used to remove the first occurrence of "B" from the given vector.
import java.util.*; public class MyClass { public static void main(String[] args) { //creating a vector Vector<String> MyVector = new Vector<String>(); //populating vector MyVector.add("A"); MyVector.add("B"); MyVector.add("C"); MyVector.add("B"); MyVector.add("D"); //printing vector System.out.println("MyVector contains: " + MyVector); //remove the first occurrence of "B" MyVector.remove("B"); //printing vector System.out.println("MyVector contains: " + MyVector); } }
The output of the above code will be:
MyVector contains: [A, B, C, B, D] MyVector contains: [A, C, B, D]
❮ Java.util - Vector