Enumerate through a Vector using Java Enumeration
Last Updated :
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
|
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
import java.util.Enumeration;
import java.util.Vector;
class GFG {
public static void main(String[] args) {
Vector<String> vector = new Vector<>();
vector.add( "Keep" );
vector.add( "Calm" );
vector.add( "and" );
vector.add( "learn" );
vector.add( "from" );
vector.add( "GFG" );
System.out.println( "The elements of the Vector is : "
+ vector);
Enumeration enumeration = vector.elements();
System.out.println(
"The output after Enumerating through the Vector : " );
while (enumeration.hasMoreElements()) {
System.out.println(enumeration.nextElement());
}
}
}
|
Output
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
import java.util.Enumeration;
import java.util.Vector;
class GFG {
public static void main(String[] args) {
Vector<Double> vector = new 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 );
System.out.println( "The elements of the Vector is : "
+ vector);
Enumeration enumeration = vector.elements();
System.out.println(
"The output after Enumerating through the Vector : " );
while (enumeration.hasMoreElements()) {
System.out.println(enumeration.nextElement());
}
}
}
|
Output
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
import java.util.Enumeration;
import java.util.Vector;
class GFG {
public static void main(String[] args) {
Vector<Object> vector = new 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 );
System.out.println( "The elements of the Vector is : "
+ vector);
Enumeration enumeration = vector.elements();
System.out.println(
"The output after Enumerating through the Vector : " );
while (enumeration.hasMoreElements()) {
System.out.println(enumeration.nextElement());
}
}
}
|
Output
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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...