java.util.Vector.remove() Method


Description

This remove(Object o) method is used to remove the first occurrence of the specified element in this Vector.If the Vector does not contain the element it remains unchanged.

Declaration

Following is the declaration for java.util.Vector.remove() method

public boolean remove(Object o)

Parameters

o − This is the element to be removed from this Vector, if present.

Return Value

The method call returns true if the Vector contained the specified element.

Exception

NA

Example

The following example shows the usage of java.util.Vector.remove() method.

package com.tutorialspoint;

import java.util.Vector;

public class VectorDemo {
   public static void main(String[] args) {

      // create an empty Vector vec with an initial capacity of 6     
      Vector<Integer> vec = new Vector<Integer>(6);

      // use add() method to add elements in the vector
      vec.add(33);
      vec.add(34);
      vec.add(22);
      vec.add(11);
      vec.add(22);
      vec.add(12);

      // let us remove the 1st element
      System.out.println("Removed element: "+vec.remove((Integer)22));     

      // lets print the elements
      System.out.println("The elements in the vector are:");     
      
      for (Integer number : vec) {         
         System.out.println("Number = " + number);
      }
   }    
}

Let us compile and run the above program, this will produce the following result.

Removed element: true
The elements in the vector are:
Number = 33
Number = 34
Number = 11
Number = 22
Number = 12
java_util_vector.htm
Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements