Collection's iterator () method

Source: Internet
Author: User

/* 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

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.