/* iterator iterator (); Gets the iterator object that the collection relies on . the iteration (traversal) of the collection through the method in the iterator. Note: This is a common way for all collections to traverse. */import java.util.*;p Ublic class collectiontest02{public static void main ( String[] args) {//Create collection Object Collection c = new arraylist ();//add Element C.add (+); // Auto Boxing c.add (3.14), //Auto Boxing c.add (FALSE),//Auto Boxing//iteration, Traverse//1. Gets the Iterator object//does not need to care about the specific type of the underlying collection, All collections rely on iterators that implement Java.util.Iterator; interface.//iterator it = c.iterator (); //iterators are interface-oriented programming. // It is a reference that holds the memory address and points to the iterator object in the heap the//java.util.linkedlist$listitr class is the iterator on which the Linkelist collection depends//java.util.abstractlist$itr The class is an iterator on which the ArrayList collection relies.//SYSTEM.OUT.PRINTLN (it); //[email protected]//[email protected]//2. Start the call method, complete the traversal, iterate. While Loop/*while (It.hasnext ()) {object element = it.next (); SYSTEM.OUT.PRINTLN (Element); //100 3.14 false}*//*boolean b = it.hasnext (); Determines if there are more elements if there is a return Trueobject o = it.next (); moves the iterator down one, and the element that points to is removed. Principle: The It.next () method must be called before calling It.hasnext (); *///for Loop for (Iterator it = c.iterator (); It.hasnext ();) {object o = it.next (); System.out.println (o);}}
This article is from the "Gaogaozi" blog, make sure to keep this source http://hangtiangazi.blog.51cto.com/8584103/1669544
Collection's iterator () method