Java iterator Demo

Source: Internet
Author: User

 PackageCn.aust.zyw.demo;ImportJava.util.Iterator;/*** Created by Zyw on 2016/2/16. * Iterator mode is the standard access method used to traverse the collection class. * It abstracts the access logic from different types of collection classes, thus avoiding exposing the internal structure of the collection to the client. * The store class inherits the Iterable interface, using the custom Hasnext (), Next () * to output the elements of array A. */ Public classtestiterable { Public Static voidMain (String args[]) {testiterable testiterable=Newtestiterable (); } testiterable () {Store store=NewStore (); System.out.println ("************");  for(String item:store) {System.out.print (item+" "); }//For (Iterator iter = Store.iterator (); Iter.hasnext ();) {//System.out.print (Iter.next () + "");//        }    }}classStoreImplementsIterable<string>{    PrivateString a[]={"Apple", "orange", "pear", "banana"}; @Override PublicIterator<string>iterator () {return NewIterator<string>() {            Private intI=0; @Override Public BooleanHasnext () {//Returns True if the iteration have more elements                if(i<a.length) {                    return true; }                return false; } @Override PublicString Next () {//Returns the next element in the iteration.                returna[i++];    }        }; }}

Java iterator Demo

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.