The removeElement
method of the Vector
class removes the first occurrence of a specific element from the vector object on which the method is invoked.
The elements after the index will be shifted one index downward. The size of the vector also decreases by 1.
The
Vector
class is a growable array of objects. You can use an integer index to access the elements ofVector
, and the size of aVector
can be increased or decreased.
public boolean removeElement(Object obj)
obj
: The element to be removed from the vector object.This method returns true
if the element to be removed is present and successfully removed from the vector. Otherwise, it returns false
.
import java.util.Vector;class RemoveElement {public static void main( String args[] ) {// Creating VectorVector<Integer> vec = new Vector<>();// add elememtsvec.add(1);vec.add(2);vec.add(1);System.out.println("The Vector is: " + vec);vec.removeElement(1);System.out.println("\nAfter removing element '1'. The Vector is: " + vec);}}
In the code above:
In line 5: We create a vector object, vec
.
In lines 8 to 10: We add three elements (1,2,1
) to the created vector object.
In line 14: We use the removeElement
method of the vector
object to remove the element 1
. The element 1
is present at index 0
and 2
. The removeElement
method will
only delete the first occurrence of the element 1
, so the element 1
at index 0
is deleted.