Java Vector removeElementAt() Method with Examples

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

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

Java Vector removeElementAt() Method with Examples

boolean removeElementAt():

This java.util.Vector.removeElementAt(int Index) method is used to remove the first occurrence of a particular element in a vector. It returns True if the element is found and removed else it returns False.

Syntax-

vectorName.removeElement(int Index)

Where,

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

Let’s see it’s use with different examples.

Method-1: Java Vector removeElementAt() Method-Example with String Type Vector

Approach:

  • Create a vector of String type.
  • Add some String elements to the vector.
  • Print the vector elements
  • Remove element from specified index of vector by using removeElementAt(int Index) method
  • Print the vector elements.

Program:

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

       // Removes the element at the specified index
        vec.removeElementAt(2);
        // Prints the vector elements
        System.out.println("The vector elements after deleting element from 2nd index are "+vec);
    }
}
Output:

The vector elements are [a, b, c, d, a]
The vector elements after deleting element from 2nd index are [a, b, d, a]

Method-2: Java Vector removeElementAt() Method-Example with Integer Type Vector

Approach:

  • Create a vector of Integer type.
  • Add some integer elements to the vector.
  • Print the vector elements
  • Remove element from specified index of vector by using removeElementAt(int Index) method
  • Print the vector elements.

Program:

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

       // Removes the element at the specified index
        vec.removeElementAt(4);
        // Prints the vector elements
        System.out.println("The vector elements after deleting element from 2nd index are "+vec);
    }
}
Output:

The vector elements are [1, 2, 3, 4, 5]
The vector elements after deleting element from 2nd index are [1, 2, 3, 4]

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: