In this section you will learn about Enumeration interface and it's implementation.
Enumerate means get one at a time. As name is telling, the Enumeration interface have functions by which you can obtain one element at a time from the collection of object.
Now a days, Iterator replaces Enumeration. Enumeration is believed as no longer use for new code. But it is still in use where some of the legacy classes such as Vector and Properties is used by several other API classes.
The methods of Enumeration are given below :
| Methods | Description |
| boolean hasMoreElements( ) | Return true when there are still element/elements left for enumerate otherwise false |
| Object nextElement( ) | Returns the next object in the Enumeration. |
Given below example uses Enumeration for traversing elements one by one:
import java.util.*;
public class EnumerationDemo {
public static void main(String args[]) {
Enumeration weekdays;
Vector days = new Vector();
days.add("Sunday");
days.add("Monday");
days.add("Tuesday");
days.add("Wednesday");
days.add("Thursday");
days.add("Friday");
days.add("Saturday");
weekdays = days.elements();
while (weekdays.hasMoreElements()) {
System.out.println(weekdays.nextElement());
}
}
}
|
Sunday Monday Tuesday Wednesday Thursday Friday Saturday |
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: Java Enumeration
Post your Comment