Java列表迭代器中的上一个 [英] previous in List Iterator of Java

查看:67
本文介绍了Java列表迭代器中的上一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Java的列表迭代器,但我不了解以前的方法是如何工作的. 我得到链接列表"B"的下一个元素,然后尝试获取前一个元素"A",但得到"B".

I use the list iterator of Java, and I don't understand how does the previous method work. I get the next element of the Linked list "B" and then I try to get the previous element "A" but I get "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());

输出为"A","B","B".

The output is "A", "B", "B".

为什么不起作用,它在幕后如何起作用?

Why doesn't it work, and how does it work behind the scenes?

推荐答案

来自JavaDocs:

From JavaDocs:

返回列表中的上一个元素并移动光标位置 向后.可以重复调用此方法以遍历 向后列出清单,或与对next的呼叫进行来回混在一起. (请注意,交替调用下一个和上一个将返回相同的 元素.)

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. (Note that alternating calls to next and previous will return the same element repeatedly.)

这里重要的部分是:(请注意,交替调用next和Previous将重复返回相同的元素.)

这篇关于Java列表迭代器中的上一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆