Enumeration is an interface introduced in 1.0V(legacy) used to retrieve data one by one
from Vector only. Using Enumeration object we will do this. There is a method elements()
invoking by Vector object, then we will get Enumeration object.
Enumeration e = v.elements();
Here v is any Vector object.
Enumeration defines the following 2 methods like:
1. public boolean hasMoreElements();
Tests if this enumeration contains more elements. It returns true if and only if this
enumeration object contains at least one more element to provide; otherwise it returns false.
2. public Object nextElement();
Returns the next element of this enumeration if this enumeration object has at least one more
element to provide. It returns the next element of this enumeration. It throws
NoSuchElementException - if no more elements exist.
EnumerationExample.java
import java.util.*;
class EnumerationExample
{
public static void main(String[] args)
{
Vector v=new Vector();
for(inti=1;i<=10;i++)
{
v.addElement(i);
}
System.out.println(v);
Enumeration e=v.elements();
while(e.hasMoreElements())
{
Integer i=(Integer)e.nextElement();
if(i%2==0)
System.out.println(i);
}
}
}
Output
[1,2,3,4,5,6,7,8,9,10]
2
4
6
8
10
Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.
We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc