Enumerate through a Vector using Java Enumeration - GeeksforGeeks

07 Jan, 2021
In the Java Enumeration class, all the listed constants are public, static, and final by default. Now after creating a Vector if we want to enumerate through the Vector then at first, we must get an Enumeration of the Vector’s element, and to do, so we use the elements() method. This method is a member function of java.util.Vector<E> class. The elements() method returns a reference to an object which implements java.util.Enumeration class and therefore we are able to use the hasMoreElements() and nextElement() method which helps us to enumerate through a Vector.
Declaration
public Enumeration<Object> elements()
Syntax:
Enumeration enu = Vector.elements()
Parameters: The method does not take any parameters.
Return value: The method returns an enumeration of the values of the Vector.
Method
Returns 
Example 1:
Java
// Java program to Enumerate through a Vector   import java.util.Enumeration;import java.util.Vector;  class GFG {    public static void main(String[] args) {                // Creating an object of Vector which contains          // String type elements        Vector<String> vector = new Vector<>();          // Adding values to the Vector        vector.add("Keep");        vector.add("Calm");        vector.add("and");        vector.add("learn");        vector.add("from");        vector.add("GFG");          // Displaying the values of the vector        System.out.println("The elements of the Vector is : "                           + vector);          // Creating the Enumeration of the Vector elements.        Enumeration enumeration = vector.elements();          // Now Enumerating through the Vector and           // printing each enumeration constant.        System.out.println(          "The output after Enumerating through the Vector : ");        while (enumeration.hasMoreElements()) {            System.out.println(enumeration.nextElement());        }    }}
The elements of the Vector is : [Keep, Calm, and, learn, from, GFG]
The output after Enumerating through the Vector : 
Keep
Calm
and
learn
from
GFG
Example 2:
Java
// Java program to Enumerate through a Vector   import java.util.Enumeration;import java.util.Vector;  class GFG {    public static void main(String[] args) {        // Creating an object of Vector which contains         // double type elements        Vector<Double> vector = new Vector<>();          // Adding values to the Vector        vector.add(1.2636);        vector.add(23.0258);        vector.add(266.1125);        vector.add(2548.0125);        vector.add(2154.02415);        vector.add(856.012);          // Displaying the values of the vector        System.out.println("The elements of the Vector is : "                             + vector);          // Creating the Enumeration of the Vector elements.        Enumeration enumeration = vector.elements();          // Now Enumerating through the Vector and printing         // each enumeration constant.        System.out.println(                    "The output after Enumerating through the Vector : ");        while (enumeration.hasMoreElements()) {            System.out.println(enumeration.nextElement());        }    }}
The elements of the Vector is : [1.2636, 23.0258, 266.1125, 2548.0125, 2154.02415, 856.012]
The output after Enumerating through the Vector : 
1.2636
23.0258
266.1125
2548.0125
2154.02415
856.012
Example 3:
Java
// Java program to Enumerate through a Vector   import java.util.Enumeration;import java.util.Vector;  class GFG {    public static void main(String[] args) {        // Creating an object of Vector which contains         // elements of different data types        Vector<Object> vector = new Vector<>();          // Adding values to the Vector        vector.add("Let's");        vector.add("Contribute");        vector.add("to");        vector.add('G');        vector.add('F');        vector.add('G');        vector.add(3);        vector.add(12.054574);          // Displaying the values of the vector        System.out.println("The elements of the Vector is : "                             + vector);          // Creating the Enumeration of the Vector elements.        Enumeration enumeration = vector.elements();          // Now Enumerating through the Vector and printing         // each enumeration constant.        System.out.println(            "The output after Enumerating through the Vector : ");        while (enumeration.hasMoreElements()) {            System.out.println(enumeration.nextElement());        }    }}
The elements of the Vector is : [Let's, Contribute, to, G, F, G, 3, 12.054574]
The output after Enumerating through the Vector : 
Let's
Contribute
to
G
F
G
3
12.054574
Java-Enumeration
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
Different ways of Reading a text file in Java
Constructors in Java
Exceptions in Java
Functional Interfaces in Java
Convert a String to Character array 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?