Listiterator interface analysis

xiaoxiao2021-03-06  57

Listiterator is a List iterator that inherits iterator, please see about Iterator interface: http://blog.9cbs.net/treeroot/archive/2004/09/11/101589.aspx

All methods are listed here (including the method in Iterator, and the Java source code is often done, although it is not necessary, it is really convenient).

Public Interface ListIterator Extends Iterator

1.Boolean Hasnext () inherits from the parent interface

2. Boolean next () inherits from the parent interface

3.Boolean Hasprevious () is easy to understand, there is an element in front

4. Object previous () Returns the previous element (next () returned the previous one)

5.int nextIndex () the index number of the next element

6.int PreviousIndex () Index number of an element

7.void remove () inherits from the parent interface, and it is a bit different from iterator, because it can be the element returned by next () or previous (), others

8.void Set (Object O) Alternate elements of the next () or previous ().

9.void Add (Object O) After adding an element () before the element returned, the element returned by previous ())

To illustrate the problem, here's the part of the comment excerpt follows the JDK / *** * An iterator for lists that allows the programmer * to traverse the list in either direction, modify * the list during iteration, and obtain the iterator's * current position in the list A ListIterator * has no current element;.. its cursor position always * lies between the element that would be returned by a call * to previous () and the element that would be * returned by a call to next () In a List of * Length N, There Are N 1 Valid * Index Values, from 0 to n, Inclusive. *

** Element (0) Element (2) ... Element (n) * ^ ^ ^ ^ ^ * INDEX: 0 1 2 3 n 1 ** * * Note That the {@link #Remove} And {@link #set (object)} Methods area * not defined in Terms of the cursor position; the is defined to * Operate on the last element return by a call to {@link #Next} or {@ link * #previous ()}. * /

It is righteous: Listiterator is a two-way iterator. Listiterator does not have a current element, and its current cursor is located between elements returned by call next () and previsous (). However, the examples will be a bit problem: the following example is N 1 element. If there is n elements, the cursor index is 0 ... n total N 1. Note: Romove and SET methods are not for the operation of the current cursor, but for the last next next () or previous (). Because it is just an interface.

转载请注明原文地址:https://www.9cbs.com/read-117027.html

New Post(0)