enumeration-Aging Iterations

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.