此测试程序返回评论{ 1, 2, 2, 3, 3 }
中的值,但我想对其进行更改,以便我收到{ 1, 2, 1, 2, 3, 2 }
。 我发现,是的ListIterator这样,因为光标在这样的值之间:如何使listiterator紧接着()和previous()不会返回相同的值?
^1*2*3*4*
星号*
是当光标可能是和^
是它目前是。如何改变它?
public class LinkedListIteratorTest {
public static void main(String[] args) {
LinkedList<Integer> backForw = new LinkedList<Integer>();
ListIterator<Integer> bFI;
backForw.add(1);
backForw.add(2);
backForw.add(3);
backForw.add(4);
bFI = backForw.listIterator();
System.out.println(bFI.next()); // 1
System.out.println(bFI.next()); // 2
System.out.println(bFI.previous()); // 2, how to make it return 1?
System.out.println(bFI.next()); // 2, if the last one would be 1, this
// should be 2.
System.out.println(bFI.next()); // 3
System.out.println(bFI.previous()); // 3, how to make it return 2?
}
}
我真的觉得你在这里问错了问题。为什么不问问题如何获得你想要的行为,而不是问怎么打破迭代器? –
有同样的问题。但在回答中看到正确的解释 – Sergey