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 |
---|---|
hasMoreElements() | If there exist more elements in the Enumeration then it returns true otherwise returns false. |
nextElement() | If there exists any next element in the Enumeration then it returns that element. |
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