Skip to content
Related Articles

Related Articles

Improve Article
Get Enumeration over Java Vector
  • Last Updated : 11 Jun, 2021

In java, the vector is a typical dynamic array whose size can increase or decrease. While in array the size cannot be changed after declaration. We have to include file import java.util.Vector to use Vector and store values in it. Also, import java.util.Enumeration to use enumeration.

Approach 1: Collection

In the Java Enumeration class, all the listed constants are public, static, and final by default. After creating a Vector the enumeration() method gets Enumeration over Vector.

public static <T> Enumeration <Object> enumeration (Collection <Object> c) 

is a member function of public class Collections extends Object.



The enumeration() method returns the enumeration object over the specified Collection and here the specified collection is a Vector. Hereafter getting the enumeration object over a Vector, we will use the hasMoreElements() and nextElement() methods to enumerate through a Vector.

Below is the implementation of the given approach:

Java




// Get Enumeration over Java Vector
import java.util.Collections;
import java.util.Enumeration;
import java.util.Vector;
 
class GFG {
    public static void main(String[] args)
    {
        // creating an object of Vector class
        Vector<String> vector = new Vector<>();
 
        // Adding elements to the Vector
        vector.add("Let's");
        vector.add("learn");
        vector.add("java");
        vector.add("from");
        vector.add("GFG");
 
        // printing the elements of the vector
        System.out.println(
            "The elements of the Vector is : " + vector);
 
        // getting the Enumeration object over Vector
        // the specified collection.
        Enumeration enumeration
            = Collections.enumeration(vector);
 
        // Now printing each enumeration constant
        // by enumerating through the Vector.
        System.out.println(
            "printing each enumeration constant by enumerating through the Vector:");
        while (enumeration.hasMoreElements()) {
            System.out.println(enumeration.nextElement());
        }
    }
}

 
 

Output
The elements of the Vector is : [Let's, learn, java, from, GFG]
printing each enumeration constant by enumerating through the Vector:
Let's
learn
java
from
GFG

 

Time Complexity: O(N), where N is the length of the vector.

 

 



 

Approach 2:

 

  1. We will declare the Vector object and then use v.add() to add elements to the vector.
  2. Use hasMoreElements() and then display the objects using nextElement().

Method use: 

 

  1. hasMoreElements(): It is used to enumerate if there are more elements.
  2. nextElements(): It is used to return the next object in enumeration.

Below is the implementation of the given approach: 

 

Java




// Get Enumeration over Java Vector
import java.io.*;
import java.util.Enumeration;
import java.util.Vector;
 
class GFG {
    public static void main(String[] args)
    {
 
        Vector<Integer> v = new Vector<Integer>();
 
        // v.add() is used to add elements to the vector
        v.add(1);
        v.add(2);
        v.add(3);
        v.add(4);
        // Create Enumeration
        Enumeration e = v.elements();
 
        // hasMoreElements() is used to check whether there
        // are more element to be enumerated
        while (e.hasMoreElements()) {
 
            // nextElement() is used to return the next
            // object in enumeration
            System.out.println(e.nextElement());
        }
    }
}

 
 

Output
1
2
3
4

 

Time Complexity: O(N), where N is the length of the vector.

 

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :