Enumeration interface
The enumeration interface itself is not a data structure. However, it is important for other data structures. The enumeration interface defines the means by which continuous data is obtained from a data structure. For example, enumeration defines a method named Nextelement that can be used to get the next element from a data structure that contains multiple elements.
The enumeration interface provides a standard set of methods, since enumeration is an interface whose role is limited to providing method protocols for data structures. Here is an example of use:
E is a object that implements the enumeration interface
while (E.hasmoreelements ()) {
Object o= e.nextelement ();
System.out.println (o);
}
The object that implements the interface consists of a series of elements that can be called continuously to get the elements in the enumeration enumeration object. Nextelement (). Only the following two methods are defined in the Enumertion interface.
Boolean hasmoreelemerts ()
Tests whether the enumeration enumeration object also contains elements, and if true, indicates that there are at least one element.
· Object nextelement ()
If the Bnumeration enumeration object also contains elements, the method obtains the next element in the object.
ImportJava.util.*;classdemoenumeration{ Public Static voidMain (string[] args) {//instantiating an object of type MydatastructMydatastruct mysatastruct=Newmydatastruct (); //Gets the enumeration object that describes the Mydatastruct type ObjectEnumeration myenumeration =Mydatastruct.getenum (); //use an object loop to display each element in an object of type Mydatastruct while(Myenumeration.hasmoreelements ()) System.out.println (Myenumeration.nextelement ()); } }//Myenumeration class implements enumeration interfaceclassMyenumeratorImplementsenumeration{intCount//counter intLength//the length of the stored arrayObject[] DataArray;//storing a reference to an array of data//ConstructorsMyenumeration (intCountintlength,object[] dataarray) { This. Count =count; This. length=length; This. Dataarray=dataarray; } Public Booleanhasmoreelements () {return(count<length); } PublicObject nextelement () {returndataarray[count++];}}//the Mydatastruct class is used to instantiate a simple, enumeration object that can be supplied//to the data result object used by the programclassmydatasttuct{string[] data; //Constructorsmydatastruct () {data=NewString[4] data[0] = "zero"; data[1]= "One"; data[2] = "both"; data[3]= "three"; }//returns a enumeration object to the user programEnumeration Getenum () {return NewMyenumeration (0, Data.length,data); }
enumeration-Aging Iterations