In this section you will learn about Enumeration interface and it's implementation.
Ads
TutorialsIn 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 |
Posted on: August 2, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles
Advertisements
Ads
Ads
Discuss: Java Enumeration
Post your Comment