Detailed explanation of iterator and listiterator instances in Java

Scope and relationship of iterator and listiterator:

(1) Iterator can be used to implement overlapping interface list, ArrayList, LinkedList, map, etc. (2) Listiterator, as its name implies, is used to iterate list and implement ArrayList and LinkedList. (3) As can be seen from the source code or API documentation, iterator is the parent class of listiterator.

A new method has been added to listiterator.

Iterator implicit cursor position instance analysis:

As shown in the figure above, there are four elements a, B, C and D in the set list to be iterated by the iterator. When calling XXX Iterator () or XXX When using the listiterator() method,

Iterator points to iterator_ The location of one when calling XXX When using the next () method, the iterator position points to iterator_ Two position.

The iterator points not directly to the element, but between the elements.

Methods in iterator and listiterator:

------Methods in iterator:

Summary of official API methods:

Method analysis:

Hasnext(): determines whether there is an element at the next position indicated by the element iterator in the collection. If yes, it returns true; otherwise, it returns false

Next (): returns the value of the next element at the position pointed to by the iterator.

Remove (): deletes the value retrieved by the iterator through the iterator.

------Methods in listiterator:

Summary of official API methods:

Method analysis:

Add (EE): inserts the specified element into the collection. The insertion position is related to the last operation of the next () or previous () method.

If the last operation is next (), insert the element after the element taken out by the next. If previous (), insert the element before the element taken out.

Hasnext(): when the collection is traversed in positive order, judge whether the next element indicated by the implicit cursor exists. If it exists, return true; otherwise, return false.

Hasprevious(): during reverse traversal of the collection, judge whether the previous element of the implicit cursor exists. If it exists, return true; otherwise, return false.

Next (): returns the next element value at the position indicated by the implicit cursor.

Nextindex(): returns the index value of the next element at the position indicated by the implicit cursor.

Previous(): returns the previous element value of the position indicated by the implicit cursor.

Previousindex(): returns the index value of the last position element indicated by the implicit cursor.

Remove(): deletes elements from the collection, not all elements, but the elements returned by the last operation next() or previous(),

If next() or previous() does not return a value, an error is reported.

Set (EE): replace the element returned by next () or previous () with E from the list. For example, next () is taken as a, and a can be replaced with one through set.

Example 1:

Example 2:

summary

The above is all about the detailed explanation of iterator and listiterator instances in Java. I hope it will be helpful to you. Interested friends can continue to refer to other related topics on this site. If there are deficiencies, please leave a message to point out. Thank you for your support!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>