Home Java Beginners Java Enumeration
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Java Enumeration

Advertisement
Java Enumeration

Java Enumeration

     

An Enumeration is an object that provides you element one at a time. This is passed through using a collection, usually of unknown size. The element is to be traversed only once at a time. you can't change the object or value  in the collection,as Enumeration is read only and has forward facility.

Method Used in Enumeration

   Method     Description
 nextElement ( ) Return the next object in Collection class
hasMoreElements ( ) Return true until the last object in Collection has returned by the next   element   method

Let Us Understand  Enumeration Example

We have declared a public class EnumerationTest,Inside the class we have a main method through which we assign an memory to the object of vector class. The vector class object  is month names that is used to add the object  in the collection class. 

We have some method in enumeration-

1) has more element-Return true till the last object in collection  returned by the next element.

2) next element  -Return the next object in collection class

import java.util.Vector;
import java.util.Enumeration;
public class EnumerationTest
{
public static void main(String args[]) 
{
Enumeration months;
Vector monthNames = new Vector();
monthNames.add("January");
monthNames .add("Febrary");
monthNames.add("March");
monthNames.add("April");
monthNames.add("May");
monthNames.add("June");
monthNames.add("july");
monthNames.add("August");
monthNames.add("September");
monthNames.add("October");
monthNames.add("November");
monthNames.add("December");
months = monthNames.elements();
while (months.hasMoreElements())
System.out.println(months.nextElement()); 

}
}

Output on Command Prompt

C:\saurabh>java EnumerationTest
January
Febrary
March
April
May
June
july
August
September
October
November
December
Advertisement

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: April 17, 2011

Ask Questions?    Discuss: Java Enumeration  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com