The first is loop traversal, common for and while. More familiar, not written.
Then the foreach format for (type name Variable name: Collection name)
and an iterator iterator
The iterator functionality in Java is relatively simple and can only be moved one way:
(1) Use method iterator () requires the container to return a iterator. The first time you call Iterator's next () method, it returns the first element of a sequence. Note: The iterator () method is an Java.lang.Iterable interface that is inherited by collection.
(2) Use Next () to get the next element in the sequence. The first time you call Iterator's next () method, it returns the first element of a sequence.
(3) Use Hasnext () to check if there are elements in the sequence.
(4) use remove () to delete the newly returned element of the iterator.
List L = new ArrayList (); L.add ("AA"); L.add ("BB"); L.add ("CC"); for (Iterator iter = L.iterator (); Iter.hasnext ();) { String str = (string) iter.next (); System.out.println (str); }/* Iterator for While loop Iterator iter = L.iterator (); while (Iter.hasnext ()) { string str = (string) iter.next (); System.out.println (str); } */
Several methods of traversal