Set output Interface-iterator iteration output-old enumeration output: enumeration

Source: Internet
Author: User

1. Iterator iteration output (95%)

Iterator is the primary interface for the collection output, the interface is defined as follows:

 Public Interface Iterator<e> {        publicboolean//public         E Next () ;   // }        

Implementation code:

1  PackageCn.demo;2 3 Importjava.util.ArrayList;4 ImportJava.util.Iterator;5 Importjava.util.List;6 7  Public classTest {8      Public Static voidMain (string[] args)throwsException {9list<string> all =NewArraylist<string>();TenAll.add ("Java"); OneAll.add ("JSP"); AAll.add ("Oracle"); -Iterator<string> iter =all.iterator (); -      while(Iter.hasnext ()) { theString str =Iter.next (); - System.out.println (str); -     } -     } +}

Results:

Java
Jsp
Oracle

2. Old enumeration output: enumeration (4.96%)

This interface is defined as follows:

 Public Interface Enumeration<e> {        publicboolean//  Determine if there is a next element                                       Public // get current element }     

The collection interface does not define a method for acquiring a enumeration interface object, and the instantiated object of this interface relies on the vector class.

There are methods for defining this class: public enumeration<e> elements ();

The code is as follows:

1  PackageCn.demo;2 3 Importjava.util.Enumeration;4 ImportJava.util.Vector;5 6  Public classTest {7      Public Static voidMain (string[] args)throwsException {8vector<string> all =NewVector<string>();9All.add ("Java");TenAll.add ("JSP"); OneAll.add ("Oracle"); AEnumeration<string> ENU =all.elements (); -          while(Enu.hasmoreelements ()) { - System.out.println (Enu.nextelement ()); the         } -     } -}

Results:

Java
Jsp
Oracle

Summary: 1, iterator belongs to set the most standard practice, two methods: Hashnext (), Next ();

2, enumeration belongs to the vector legacy method, two methods: hasMoreElements (), Nextelement ().

Set output Interface-iterator iteration output-old enumeration output: enumeration

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.