Java->iterator (iteration)

Source: Internet
Author: User

iterator iterators Overview

There are a number of collections in Java that are stored differently when storing elements. We're going to take out the elements in these collections, which can be done in a common way.

Collection The general acquisition of the elements of the collection: before taking the element to determine whether there are any elements in the set, if there is, take this element out, continue to judge, if there is another out. All the elements in the collection have been taken out all the time. This method of extraction is known as an iterative term.

This method of fetching elements in the collection is described in the iterator interface. The common methods for iterator interfaces are as follows:

Hasnext () method: used to determine whether the next element in the collection can be iterated. If it returns true, the description can be iterated.

Next () method: used to return the next element of the iteration and move the pointer back one bit. (Can be thought of as a global variable, each time the pointer moves back one bit, and then when the pointer moves backward one bit, does not return to zero)

Code representation of the iterator iterative approach

The collection interface describes an abstract method iterator method, all collection subclasses implement this method, and have their own iteration form.

For a code demonstration:

1, Create the collection object .

collection<string> coll = new arraylist<string> ();

Coll.add ("ABC1");

Coll.add ("ABC2");

Coll.add ("ABC3");

Coll.add ("ABC4");

2. gets the iterator object for the container . Through the iterator method.

Iterator it = coll.iterator ();

3, gets the elements in the collection using the specific iterator object . See Methods for Iterators

while (It.hasnext ()) {

System.out.println (it.next ());

}

/*

Use of the Iterator for loop form

for (Iterator it = Coll.iterator (); It.hasnext ();) {

System. out. println (It.next ());

}

*/

Note: When the collection element is taken out, if there are no elements in the collection, and the next method of the iterator continues to be used, an error java.util.NoSuchElementException without the collection element will occur.

downward transformation of collection elements

Learning here, the basic knowledge of the collection interface simple use. However, any object can be stored in the collection, then is the data stored in the original type? No, the Ascension becomes an object.

When working with collections, we need to pay attention to the following points:

The storage in the collection is actually the address of the object .

Can I store the base values in the collection? The jdk1.5 version can be stored later. Because the basic type wrapper class appears, it provides an auto-boxing operation (the base type à object), so that the elements in the collection are the wrapper class objects of the base values.

The object is lifted when stored. To use the element's unique content when it is removed, it must be transformed downward.

Collection coll = new ArrayList ();

Coll.add ("abc");

Coll.add ("AABBCC");

Coll.add (1);

Iterator it = Coll.iterator ();

while (It.hasnext ()) {

Because the elements are all promoted to the object type after they are stored in the collection

When you need to use a subclass object-specific method, you need to shift down

String str = (string) it.next ();

System. out. println (Str.length ());

}

Note: If there are multiple objects in the collection, a type conversion exception occurs when you make a downward transition.

tip:Iterator interface can also be used <> to control the type of the iteration element . The code demonstrates the following:

Collection<String> coll = new arraylist<string> ();

Coll.add ("abc");

Coll.add ("AABBCC");

Coll.add ("Cat");

Iterator<string> it = Coll.iterator ();

while (It.hasnext ()) {

String str = It.next ();

When you use iterator<string> to control element types, you do not need a strong turn. Gets the element that is directly the string type

System. out. println (Str.length ());

}

Java->iterator (iteration)

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.