public interface Enumeration<E>
An object that implements the enumeration interface. It generates a series of elements and generates one at a time. Continuous callnextElement
Method returns a series of continuous elements.
For exampleVector <E> VYou can use the following methods for all elements:
for (Enumeration<E> e = v.elements(); e.hasMoreElements();) System.out.println(e.nextElement());
These methods are enumerated by vector elements, hash table keys, and values in the hash table. Enumeration is also used to specify the input streamSequenceInputStream
.
Note: The functions of this interface are the same as those of the iterator interface. In addition, the iterator interface adds an optional remove operation and uses a shorter method name. The iterator interface rather than the enumeration interface should be preferred for the new implementation.
Import java. util. enumeration;
Public class demoenumeration {
Public static void main (string [] ARGs ){
// Instantiate an object of the mydatastruct type
Mydatastruct = new mydatastruct ();
// Obtain the enumeration object that describes objects of the mydatastruct type.
Enumeration <Object> myenumeration = mydatastruct. getenum ();
// Use an object to display every element of an object of the mydatastruct type cyclically
While (myenumeration. hasmoreelements ())
System. Out. println (myenumeration. nextelement ());
}
}
// The myenumeration class implements the enumeration interface.
Class myenumeration implements enumeration <Object>
{
Int count; // counter
Int length; // The length of the stored Array
Object [] dataarray; // reference for storing data Arrays
Myenumeration (INT count, int length, object [] dataarray ){
This. Count = count;
This. Length = length;
This. dataarray = dataarray;
}
Public Boolean hasmoreelements (){
Return (count <length );
}
Public object nextelement (){
Return dataarray [count ++];
}
}
// The mydatastruct class is used to instantiate a simple enumeration object.
// The data result object for the user program
Class mydatastruct {
String [] DATA = NULL;
Mydatastruct (){
Data = new string [4];
Data [0] = "zero ";
Data [1] = "one ";
Data [2] = "two ";
Data [3] = "three ";
}
// Return an enumeration object to the application
Enumeration <Object> getenum (){
Return new myenumeration (0, Data. length, data );
}
}
Application 1:
Import java. util. enumeration;
Import java. util. vector;
Public class vectortest {
Public static void main (string [] ARGs ){
Vector <string> myvector = new vector <string> ();
Myvector. addelement ("hello1 ");
Myvector. addelement ("hello2 ");
Myvector. addelement ("hello3 ");
For (enumeration <string> enu = myvector. Elements (); enu. hasmoreelements ();){
// Output all elements of myvector. You can use the following methods:
System. Out. println (enu. nextelement ());
// Nextelement () If this enumeration object has at least one available element, the next element of this enumeration is returned.
}
Enumeration <string> enu = myvector. Elements ();
While (enu. hasmoreelements () {// hasmoreelements () test whether this enumeration contains more elements
System. Out. println (enu. nextelement ());
}
System. Out. println (myvector. getclass (). getname () + '@' + integer. tohexstring (myvector. hashcode ()));
}
}
Application 2:
Package enumeration;
Import java. util. enumeration;
Import java. util. hashtable;
Public class hashtabletest {
Public static void main (string [] ARGs ){
Hashtable <string, string> hash = new hashtable <string, string> ();
Hash. Put ("one", "1 ");
Hash. Put ("two", "2 ");
Hash. Put ("three", "3 ");
Enumeration <string> enumkey = hash. Keys ();
While (enumkey. hasmoreelements ()){
String STR = (string) enumkey. nextelement ();
System. Out. println ("--------" + Str );
System. Out. println ("=========" + hash. Get (STR ));
If ("1". Equals (hash. Get (STR )))
Hash. Remove (STR );
}
System. Out. println ("-ssssssssssssssssssss-" + hash. Get ("three "));
Enumkey = hash. Keys ();
While (enumkey. hasmoreelements ()){
String STR = (string) enumkey. nextelement ();
System. Out. println ("--------" + Str );
System. Out. println ("=========" + hash. Get (STR ));
}
}
}
Application 3:
<HTML>
<Head>
<Base href = "<% = basepath %>">
<Title> </title>
</Head>
<Body>
<Center>
<H2>
Volunteer registration
</H2>
<Form action = "dosubmit. jsp" method = "Post">
<Table border = 0>
<Tr valign = "TOP">
<TD align = "right">
Name:
</TD>
<TD align = "Left">
<Input type = "text" name = "name" maxlength = "50" size = "40"
Value = "chenxd"/>
</TD>
</Tr>
<Tr valign = "TOP">
<TD align = "right">
Email:
</TD>
<TD align = "Left">
<Input type = "text" name = "email" maxlength = "80" size = "40"
Value = "yourname@beijing2008.com"/>
</TD>
</Tr>
<Tr valign = "TOP">
<TD align = "right">
Gender:
</TD>
<TD align = "Left">
<Input type = "radio" name = "sex" value = "male" Checked = "true"/>
Male
<Input type = "radio" name = "sex" value = "female"/>
Female
</TD>
</Tr>
<Tr valign = "TOP">
<TD align = "right">
What kind of foreign language will it take:
</TD>
<TD align = "Left">
<Input type = "checkbox" name = "Lang" value = "English" Checked = "true"/>
English
<Input type = "checkbox" name = "Lang" value = "French"/>
French
<Input type = "checkbox" name = "Lang" value = "Russian"/>
Russian
<Input type = "checkbox" name = "Lang" value = "German"/>
German
</TD>
</Tr>
<Tr valign = "TOP">
<TD align = "right">
Registered region:
</TD>
<TD align = "Left">
<Select name = "regtelephone">
<Option value = "800-810-2008">
Beijing
</Option>
<Option value = "800-820-2008" selected = "true">
Shanghai
</Option>
</SELECT>
</TD>
</Tr>
<Tr valign = "center">
<TD align = "right">
Personal Profile:
</TD>
<TD align = "Left">
<Textarea rows = "5" Cols = "40" name = "Intro"> </textarea>
</TD>
</Tr>
<Tr valign = top>
<TD colspan = "2" align = "center">
<Input type = "Submit" value = "register"/>
<Input type = "reset" value = "reset"/>
</TD>
</Tr>
</Table>
</Form>
</Center>
</Body>
</Html>
<HTML>
<Head>
<Base href = "<% = basepath %>">
<Title> User Registration: Use the request object to obtain user submitted data </title>
</Head>
<Body>
<H2>
The content you submitted is as follows:
</H2>
<%
String current_param = "";
Request. setcharacterencoding ("GBK ");
Enumeration <string> Params = request. getparameternames ();
While (Params. hasmoreelements ()){
Current_param = (string) Params. nextelement ();
Out. Print ("parameter name:" + current_param );
String [] values = request. getparametervalues (current_param );
For (INT I = 0; I <values. length; I ++)
Out. Print ("parameter value:" + values [I]);
Out. Print ("<br> ");
}
%>
</Body>
</Html>