Iterator (iterator)

Source: Internet
Author: User

An iterator inside the Iterator:java that is used primarily to take the value inside the collection container.

Iterator Common methods:

1. Iterator (): Requires the container to return a iterator;

2. Hasnext (): Checks whether there are any elements that are not traversed in the collection;

3. Next (): Gets the next element in the collection;

4. Remove (): Removes the newly returned element of the iterator;

Iterator using:

set<string> iteset = new hashset<string> (); New HashSet Gather
Iteset.add ("first"); Add elements
Iteset.add ("second");
Iteset.add ("third");

Iterator<string> iter = Iteset.iterator (); Get the Iterator
while (Iter.hasnext ()) {//Iterator have next element
String str = (string) iter.next (); Get the next element in Iterator
System.out.println (str);
}

Note: The collection list and set inherit from the interface collection. There are two elements to get a collection in a list: 1. Iterator iterator traversal; 2. Get (int index); There is only one way to get the elements in the set collection: iterator iterator traversal, there is no method such as get (int index) in the set class.

Iterator (iterator)

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.