java.util.Vector.remove() Method


Description

The remove(int index) method is used to remove the element at the specified position in this Vector.It shifts any subsequent elements to the left (subtracts one from their indices).It alsorReturns the element that was removed from the Vector.

Declaration

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

public E remove(int index)

Parameters

index − This is the index of the element to be removed.

Return Value

The method call returns the element that was removed.

Exception

ArrayIndexOutOfBoundsException − This exception is thrown if the index is out of range.

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 4      
      Vector<Integer> vec = new Vector<Integer>(4);

      // use add() method to add elements in the vector
      vec.add(4);
      vec.add(3);
      vec.add(2);
      vec.add(1);

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

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

Removed element: 3
java_util_vector.htm
Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements