beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

How to remove Vector elements using index in java example

By Chaitanya Singh | Filed Under: Java Collections

In this tutorial, we will learn how to remove elements from Vector using index. We will be using remove(int index) method of Vector class.

public E remove(int index): Removes the element at the specified position in this Vector. Shifts any subsequent elements to the left (subtracts one from their indices). Returns the element that was removed from the Vector.

Example

Index starts from 0 so if we are calling remove(2), it would remove the 3rd element from Vector.

import java.util.Vector;
public class RemoveExample {
 public static void main(String[] args) {
 
    // Creating a Vector of Strings
    Vector<String> vector = new Vector<String>();
 
    //Adding elements to the Vector
    vector.add("C++");
    vector.add("Java");
    vector.add("Cobol");
    vector.add("C");
    vector.add("Oracle");

    System.out.println("Vector elements before remove(): ");
    for(int i=0; i < vector.size(); i++)
    {
       System.out.println(vector.get(i));
    }
 
    // Removing 3rd element from Vector
    Object obj = vector.remove(2);
 
    System.out.println("\nElement removed from Vector is:");
    System.out.println(obj);
 
    System.out.println("\nVector elements after remove():");
    for(int i=0; i < vector.size(); i++)
    {
       System.out.println(vector.get(i));
    }
 }
}

Output:

Vector elements before remove(): 
C++
Java
Cobol
C
Oracle

Element removed from Vector is:
Cobol

Vector elements after remove():
C++
Java
C
Oracle

This was the example to remove Vector elements from specified index.

Enjoyed this post? Try these related posts

  1. Java – Check if a particular key exists in HashMap example
  2. How to sort ArrayList in descending order in Java
  3. How to serialize ArrayList in java
  4. Difference between List and Set in Java
  5. Copy all the elements of one Vector to another Vector example
  6. Java ArrayList ensureCapacity() Method example

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

  • Java Tutorial
  • OOPs Concepts

Java Collections

  • ArrayList
  • LinkedList
  • ArrayList vs LinkedList
  • Vector
  • ArrayList vs Vector
  • HashMap
  • TreeMap
  • LinkedHashMap
  • HashSet
  • TreeSet
  • LinkedHashSet
  • Hashtable
  • HashMap vs Hashtable
  • Queue
  • PriorityQueue
  • Deque & ArrayDeque
  • Iterator
  • ListIterator
  • Comparable Interface
  • Comparator Interface
  • Java Collections Interview Q

MORE ...

  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap