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.
public Enumeration<Object> elements()
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.
|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.|
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
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
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
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.