简体   繁体   English

Java列表迭代器中的上一个

[英]previous in List Iterator of Java

I use the list iterator of Java, and I don't understand how does the previous method work. 我使用Java的列表迭代器,但我不了解以前的方法是如何工作的。 I get the next element of the Linked list "B" and then I try to get the previous element "A" but I get "B". 我得到链接列表“ B”的下一个元素,然后尝试获取前一个元素“ A”,但得到“ B”。

LinkedList<Character> abc = new LinkedList<Character>();
abc.add('A');
abc.add('B');
ListIterator<Character> iterator = abc.listIterator();
System.out.println(iterator.next());
System.out.println(iterator.next());
System.out.println(iterator.previous());

The output is "A", "B", "B". 输出为“ A”,“ B”,“ B”。

Why doesn't it work, and how does it work behind the scenes? 为什么它不起作用?它在后台如何起作用?

From JavaDocs: 从JavaDocs:

Returns the previous element in the list and moves the cursor position backwards. 返回列表中的上一个元素,并将光标位置向后移动。 This method may be called repeatedly to iterate through the list backwards, or intermixed with calls to next to go back and forth. 可以重复调用此方法以向后遍历列表,也可以将其与对next的调用混合以进行往复。 (Note that alternating calls to next and previous will return the same element repeatedly.) (请注意,交替调用next和Previous将重复返回相同的元素。)

Important part here is: (Note that alternating calls to next and previous will return the same element repeatedly.) 这里重要的部分是:( 请注意,交替调用next和Previous将重复返回相同的元素。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM