Get Enumeration over Java Vector - GeeksforGeeks

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 Vectorimport 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());        }    }}
  
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:
 
We will declare the Vector object and then use v.add() to add elements to the vector.Use hasMoreElements() and then display the objects using nextElement().
We will declare the Vector object and then use v.add() to add elements to the vector.
Use hasMoreElements() and then display the objects using nextElement().
Method use: 
 
hasMoreElements(): It is used to enumerate if there are more elements.nextElements(): It is used to return the next object in enumeration.
hasMoreElements(): It is used to enumerate if there are more elements.
nextElements(): It is used to return the next object in enumeration.
Below is the implementation of the given approach: 
 
Java
// Get Enumeration over Java Vectorimport 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());        }    }}
  
1
2
3
4
 
Time Complexity: O(N), where N is the length of the vector.
 
akshaysingh98088
Java-Vector
Picked
Technical Scripter 2020
Java
Java Programs
Technical Scripter
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Constructors in Java
Exceptions in Java
Functional Interfaces in Java
Different ways of Reading a text file in Java
Java Programming Examples
Convert Double to Integer in Java
Implementing a Linked List in Java using Class
How to Iterate HashMap in Java?
Program to print ASCII Value of a character