Home Java Example Java Util Java Enumeration
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Java Enumeration

Advertisement
In this section you will learn about Enumeration interface and it's implementation.

Java Enumeration

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.

EXAMPLE

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());
}
}
}

OUTPUT

Sunday

Monday

Tuesday

Wednesday                             

Thursday

Friday

Saturday

Download Source Code

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: August 2, 2011

Ask Questions?    Discuss: Java Enumeration  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Padma
May 29, 2013
program

This site is being very useful. If it is possible, please send me more programs on each topic to my mail id.
DMCA.com