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