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

Vector Enumeration example in Java

By Chaitanya Singh | Filed Under: Java Collections

In this example, we are iterating a Vector using Enumeration. The steps are as follows:
1) Create a Vector object
2) Add elements to vector using add() method of Vector class.
3) Call elements() method to get the Enumeration of specified Vector
4) Use hashMoreElements() and nextElement() Methods of Enumeration to iterate through the Vector.

Example: Complete code

import java.util.Vector;
import java.util.Enumeration;
 
public class VectorEnumerationExample {
 public static void main(String[] args) {
    // Create a Vector 
    Vector<String> vector = new Vector<String>();
 
    // Add elements into Vector
    vector.add("Chaitanya");
    vector.add("Shubham");
    vector.add("Apoorv");
    vector.add("Jin");
    vector.add("Jacob");
 
    // Get Enumeration of Vector elements 
    Enumeration en = vector.elements();
 
    /* Display Vector elements using hashMoreElements()
     * and nextElement() methods. 
     */
    System.out.println("Vector elements are: ");
    while(en.hasMoreElements())
       System.out.println(en.nextElement());
  }
}

Output:

Vector elements are: 
Chaitanya
Shubham
Apoorv
Jin
Jacob

Enjoyed this post? Try these related posts

  1. Java ArrayList remove(int index) Method example
  2. ArrayList in java with example programs – Collections Framework
  3. How to get the Sub Map from TreeMap example – Java
  4. How to loop ArrayList in Java
  5. Java – Remove first and last element from LinkedList example
  6. Java ArrayList lastIndexOf(Object 0bj) Method example

Comments

  1. manda says

    November 3, 2016 at 3:41 AM

    With JDK6 the below line from your code doesn’t get compiled

    // Get Enumeration of Vector elements
    Enumeration en = vector.elements();

    Reply

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