Java Vector remove() Method with Examples

In the previous article we have discussed Java Vector addAll() Method with Examples

In this article you will see the use of Vector remove() method in Java along with suitable examples.

Java Vector remove() Method with Examples

Let’s see Vector remove() method along with examples.

remove(int Index) method:

This java.util.Vector.remove(int Index) method removes the element from the vector at a specified index and return the removed element itself.

vectorName.remove(Int index);

Where,

  • vectorName refers to the name of your Vector.
  • int Index refers to the index of vector element which you wants to remove.

Approach:

  • Initialize a vector.
  • Add some elements to the vector.
  • Print the vector.
  • Remove one element by passing its index to the remove(int Index) method.
  • Print the modified vector.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args) 
    {
        // Create a Vector of string datatype
        Vector<Integer> vec = new Vector<Integer>();
        // Adding some elements to the vector
        vec.add(100);
        vec.add(200);
        vec.add(300);
        vec.add(400);
        vec.add(500);
        // Prints the vector elements
        System.out.println("The vector elements are "+vec);

        // Remove element from index
        Integer removed_element=vec.remove(2);
        System.out.println("Removed element from vector is "+removed_element);
        // Print the vector elements
        System.out.println("The new vector after removing element from 2nd index is"+vec);
    }
}
Output:

The vector elements are [100, 200, 300, 400, 500]
Removed element from vector is300
The new vector after removing element from 2nd index is[100, 200, 400, 500]

remove(Object Element) method:

This java.util.Vector.remove(Object element) method removes a particular element from the vector and it returns True if the element is removed from vector else it returns False.

vectorName.remove(Object element);

Where,

  • vectorName refers to the name of your Vector.
  • Object element refers to the element that you want to remove from vector.

Approach:

  • Initialize a vector.
  • Add some elements to the vector.
  • Print the vector.
  • Remove one element from vector by passing the element itself to the remove(Object element) method.
  • Print the modified vector.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args) 
    {
        // Create a Vector of string datatype
        Vector<Integer> vec = new Vector<Integer>();
        // Adding some elements to the vector
        vec.add(100);
        vec.add(200);
        vec.add(300);
        vec.add(400);
        vec.add(500);
        // Prints the vector elements
        System.out.println("The vector elements are "+vec);
        // Remove element by element
        vec.remove((Integer)500);
        // Print the vector elements
        System.out.println("The new vector after removing an element is "+vec);
    }
}
Output:

The vector elements are [100, 200, 300, 400, 500]
The new vector after removing an element is [100, 200, 300, 400]

Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.

Related Java Programs: