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 |