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

Remove Vector element – Java example

By Chaitanya Singh | Filed Under: Java Collections

In this example we will see how to remove elements from Vector. We will be using remove(Object o) method of Vector API in order to remove specified elements.
public boolean remove(Object o): Removes the first occurrence of the specified element from Vector If the Vector does not contain the element, it is unchanged.

Example

In this example we are removing two String values from Vector of Strings. The steps are as follows:
1) Create a Vector
2) Add elements to the Vector using add(Element e) method of Vector class.
3) Remove elements using remove(Object o) method of Vector.

import java.util.Vector;
public class RemoveFromVector {
  public static void main(String[] args) {
     // Creating a Vector of String Elements
     Vector<String> vector = new Vector<String>();
 
     //Adding elements to the Vector
     vector.add("Harry");
     vector.add("Steve");
     vector.add("Vince");
     vector.add("David");
     vector.add("Matt");

     System.out.println("Vector elements before remove(): ");
     for(int i=0; i < vector.size(); i++)
     {
        System.out.println(vector.get(i));
     }
 
     // Removing Harry
     vector.remove("Harry");
     // Removing Matt
     vector.remove("Matt");
 
     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(): 
Harry
Steve
Vince
David
Matt

Vector elements after remove(): 
Steve
Vince
David

The remove(Object o) method returns boolean value. It returns true if specified element is present in Vector else false.

Enjoyed this post? Try these related posts

  1. Java ArrayList add() Method Example
  2. Java ArrayList set() Method example
  3. TreeSet Class in Java with example
  4. Java ArrayList isEmpty() Method example
  5. Deque Interface in Java Collections
  6. Get size of Hashtable example in Java

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