Java Basic Knowledge Hardening Collection Framework note the traversal of the 07:collection collection iterator traversal

Source: Internet
Author: User

1. Collection iterator:

1 Iterator  Iterator (): iterator, private traversal of the collection

2. code example:

 Packagecn.itcast_03;Importjava.util.ArrayList;Importjava.util.Collection;ImportJava.util.Iterator;/** Iterator Iterator (): iterators, private traversal of collections
* Iterator (iterator): * Object Next (): Gets the element and moves to the next position . * Nosuchelementexception: No such element, because you have found the last. * Boolean Hasnext (): returns True if there are still elements that can iterate. */ Public classIteratordemo { Public Static voidMain (string[] args) {//To create a collection objectCollection C =NewArrayList (); //Creating and adding elements//String s = "Hello"; //C.add (s);C.add ("Hello"); C.add ("World"); C.add ("Java"); //Iterator Iterator (): iterator, private traversal of the collectionIterator it = C.iterator ();//The actual return is definitely a subclass object, here is the polymorphic//Object obj = It.next (); //System.out.println (obj); //System.out.println (It.next ()); //System.out.println (It.next ()); //System.out.println (It.next ()); //System.out.println (It.next ()); //The last one should not be written, so we should have a judgment before each acquisition//Judging whether there is the next element, there is to get it, not to ignore it//if (It.hasnext ()) {//System.out.println (It.next ()); // } //if (It.hasnext ()) {//System.out.println (It.next ()); // } //if (It.hasnext ()) {//System.out.println (It.next ()); // } //if (It.hasnext ()) {//System.out.println (It.next ()); // } //if (It.hasnext ()) {//System.out.println (It.next ()); // } //FINAL code while(It.hasnext ()) {//System.out.println (It.next ());String s =(String) it.next (); System.out.println (s); } }}

The results of the operation are as follows:

Java Basic Knowledge Hardening Collection Framework note the traversal of the 07:collection collection iterator traversal

Related Article

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.