java ListIterator 没有 return 预期的项目
java ListIterator does not return expected items
我有以下代码显示 ListIterator 的工作原理,但迭代器返回的项目似乎不是我所期望的。
import java.util.*;
public class IteratorExample {
public static void main(String args[]) {
ArrayList al = new ArrayList();
al.add("A");
al.add("B");
al.add("C");
ListIterator litr = al.listIterator();
System.out.print(litr.next()); // expect A
System.out.print(litr.next()); // expect B
System.out.print(litr.next()); // expect C
System.out.print(litr.previous()); // expect B
System.out.print(litr.previous()); // expect A
System.out.print(litr.next()); // expect B
System.out.print(litr.previous()); // expect A
}}
我希望看到 "ABCBABA",但示例程序给了我 "ABCCBBB"。谁能解释迭代器是如何工作的?只用迭代器就想得到结果"ABCBABA"怎么办?
如果您阅读了 java 文档中的 listIterator.previous() 方法,那么下面提到的内容可以回答您的问题:
"Returns the previous element in the list. This method may be called repeatedly to iterate through the list backwards, or intermixed with calls to next to go back and forth. (Note that alternating calls to next and previous will return the same element repeatedly.)"
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(). An iterator for a list of length n
has n+1 possible cursor positions, as illustrated by the carets (^)
below:
Element(0) Element(1) Element(2) ... Element(n-1)
positions: ^ ^ ^ ^ ^
这是理解此行为的另一种简单方法。
next() --> Return 下一个元素并将光标前进一个元素,以便现在光标指向下一个元素。
previous() --> Return 上一个元素并将光标向后移动一个元素,使光标指向上一个元素。
在创建列表后,迭代器位置如下所示:
ListIterator litr = al.listIterator();
A B C
^
System.out.print(litr.next()); // print A and move to next
A B C
^
System.out.print(litr.next()); // print B and move to next
A B C
^
System.out.print(litr.next()); // print C and move to next
A B C
^
System.out.print(litr.previous()); // print previous which is C and move backward
A B C
^
System.out.print(litr.previous()); // print previous which is B and move backward
A B C
^
System.out.print(litr.next()); // print B and move to next
A B C
^
System.out.print(litr.previous()); // print previous which is B and move backward
A B C
^
所以输出是正确的"ABCCBBB"
我有以下代码显示 ListIterator 的工作原理,但迭代器返回的项目似乎不是我所期望的。
import java.util.*;
public class IteratorExample {
public static void main(String args[]) {
ArrayList al = new ArrayList();
al.add("A");
al.add("B");
al.add("C");
ListIterator litr = al.listIterator();
System.out.print(litr.next()); // expect A
System.out.print(litr.next()); // expect B
System.out.print(litr.next()); // expect C
System.out.print(litr.previous()); // expect B
System.out.print(litr.previous()); // expect A
System.out.print(litr.next()); // expect B
System.out.print(litr.previous()); // expect A
}}
我希望看到 "ABCBABA",但示例程序给了我 "ABCCBBB"。谁能解释迭代器是如何工作的?只用迭代器就想得到结果"ABCBABA"怎么办?
如果您阅读了 java 文档中的 listIterator.previous() 方法,那么下面提到的内容可以回答您的问题:
"Returns the previous element in the list. This method may be called repeatedly to iterate through the list backwards, or intermixed with calls to next to go back and forth. (Note that alternating calls to next and previous will return the same element repeatedly.)"
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(). An iterator for a list of length n has n+1 possible cursor positions, as illustrated by the carets (^) below:
Element(0) Element(1) Element(2) ... Element(n-1)
positions: ^ ^ ^ ^ ^
这是理解此行为的另一种简单方法。
next() --> Return 下一个元素并将光标前进一个元素,以便现在光标指向下一个元素。
previous() --> Return 上一个元素并将光标向后移动一个元素,使光标指向上一个元素。
在创建列表后,迭代器位置如下所示:
ListIterator litr = al.listIterator();
A B C
^
System.out.print(litr.next()); // print A and move to next
A B C
^
System.out.print(litr.next()); // print B and move to next
A B C
^
System.out.print(litr.next()); // print C and move to next
A B C
^
System.out.print(litr.previous()); // print previous which is C and move backward
A B C
^
System.out.print(litr.previous()); // print previous which is B and move backward
A B C
^
System.out.print(litr.next()); // print B and move to next
A B C
^
System.out.print(litr.previous()); // print previous which is B and move backward
A B C
^
所以输出是正确的"ABCCBBB"