Java-preliminary Understanding-Common Object API (collection framework-iterator use)

Source: Internet
Author: User

One.

We complete the top-level interface of the set framework collection the common method of learning, ready to put the elements stored in the collection collection to take it out, because the purpose is to facilitate the use of elements to remove. The colection gives us a way to get an iterator (an iterator is a professional term that means to take out the elements in a collection).

The way to remove elements is done in a way that is now a demonstration.

New who does not matter, as long as can be new (collection interface of a lot of subclasses, pick a Create object).

Call the iterator method of the collection and return the iterator object in the collection, what type of object is returned? The interface type is returned, as shown in

Next, we get the iterator object with it. (originally just to take the element, so the operation seems very troublesome → first remember the operation steps, and then tell the specific principle)

Get the Iterator object, how to use it? Iterators themselves most clearly. (now there is a container containing a bunch of elements, now to take them out, how to take them?) To get the object that can take out the element in the container, this object is an iterator, which can help me iterate over one iteration. The last is to find the iterator's own method)

The methods in the iterator are as follows,

Hasnext returns an object, Next returns an object.

The result is ABC1, and ABC2. The first feature of the iterator method is shown here, and the next method takes the first one after the element in the collection is taken out. is called again, automatically takes the next. The reason for the auto-walk is that there is a pointer in it, it goes first, the pointer leans to the next, and then the next one.

It is possible to connect to four, but if the number of occurrences exceeds the number stored in the collection, the result is as follows, with an exception.

Invoking the next statement is repeated several times, and can be used in a looping structure, but when does the loop end? Depends on the Hasnext method.

DOS results run normally, while using while is judged every time.

What is the difference between this and the direct printing coll collection, directly printing is a large string, to take out the inside of something difficult to fetch. You can now remove any element by means of an iterator.

The use of iterators, first to judge there is no, then a fetch. (now it's all part of the story)

What is the other method of extraction?

Iterator it is a reference that can be used by it at the end of the while. It.next (); it can not be taken, although it is not available, but it is still in use, in other words, it is in memory.

As the iterator ends, it is useless and occupies a memory space. The memory space, this object we will not disappear. We can change it to another way of writing.

When this for loop is over, it disappears.

When developing, write a For loop.

The most basic use of iterators is described above.

Java-Preliminary Understanding-Common Object API (collection framework-iterator use)

Related Article

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.