Java Enumeration Interface

Source: Internet
Author: User

    • Enumeration Interface Definition
    • Enumeration interface and iterator interface usage comparison

One

1. Enumeration Interface Definition

public interface Enumeration<e> implements the enumeration interface object, which generates a series of elements, one at a time. Successive calls to the Nextelement method will return a series of contiguous elements.

For example, to output all elements of vector<e> v , you can use the following methods:

For (enumeration<e> E = v.elements (); e.hasmoreelements ();)

System.out.println (E.nextelement ());

These methods are enumerated primarily through the elements of the vector, the keys of the Hashtable, and the values in the hash table. The enumeration is also used to assign an input stream to Sequenceinputstream.

Note: The functionality of this interface is duplicated with the functionality of the Iterator interface. Additionally, the Iterator interface adds an optional remove operation and uses a shorter method name. The new implementation should prioritize the use of the Iterator interface instead of the enumeration interface .

2, two methods

hasmoreelements () to test whether this enumeration contains more elements.

nextelement () returns the next element of this enumeration if there is at least one element that can be supplied by this enumeration object.

Two

1, enumeration interface and iterator interface usage comparison, the use of both interface methods can be used to traverse the collection elements, the former is now only used in special occasions, the latter is now widely used.

2, the code is as follows:

 PackageAction;Importjava.util.Enumeration;ImportJava.util.Iterator;ImportJava.util.Vector; Public classTest { Public Static voidMain (string[] args) {Vector dayNames=NewVector (); Daynames.add ("Sunday"); Daynames.add ("Monday"); Daynames.add ("Tuesday"); Daynames.add ("Wednesday"); Daynames.add ("Thursday"); Daynames.add ("Friday"); Daynames.add ("Saturday"); //traversing with iterator iteratorsIterator it =Daynames.iterator ();  while(It.hasnext ()) {String name=(String) it.next ();        SYSTEM.OUT.PRINTLN (name); } System.out.println ("##################### Demarcation Line ####################"); //traversing using the enumeration interfaceEnumeration days =daynames.elements ();  while(Days.hasmoreelements ()) {System.out.println (days.nextelement ()); }    }}

3. The results are as follows:

sundaymondaytuesdaywednesdaythursdayfridaysaturday#####################  dividing line     #################### Sundaymondaytuesdaywednesdaythursdayfridaysaturday

Java Enumeration Interface

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.