java - ListIterator 在交替调用 next() 和 previous() 时重复元素

标签 java collections iterator listiterator

我编写了下面的程序来在列表中进行迭代,但在交替遍历 next() 和 previous() 时,它会重复该元素。我通过在打印逻辑之前放置一个指示器并将其用于额外下一个额外上一个来知道修复方法。但我想知道为什么行为是这样的以及迭代器工作背后的算法是什么。

我已经检查了 javaDoc,它的写法如下:

next
Returns the next element in the list. This method may be called repeatedly to iterate through the list, or intermixed with calls to previous to go back and forth. (Note that alternating calls to next and previous will return the same element repeatedly.)

但问题是为什么?这样做的逻辑或目的是什么?

public class IterateLinkedListUsingListIterator {

public static void main(String[] args) throws NumberFormatException,
        IOException {
    LinkedList lList = new LinkedList();

    lList.add("1");
    lList.add("2");
    lList.add("3");
    lList.add("4");
    lList.add("5");

    ListIterator itr = lList.listIterator();
    boolean ch = true;

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    while (ch) {
        System.out.println("Enter choice");
        int chi = Integer.parseInt(br.readLine());
        switch (chi) {
        case 1:
            if (itr.hasNext()) {
                System.out.println(itr.next());
            }
            break;
        case 2:

            if (itr.hasPrevious()) {
                System.out.println(itr.previous());
            }
            break;

        default:
            ch = false;
        }

    }

}

}

根据@vincrichaud的回答和java doc语句光标指向元素之间而不是元素上,为什么会这样?有什么具体原因吗?

                     Element(0)   Element(1)   Element(2)   ... Element(n-1)
cursor positions:  ^            ^            ^            ^                  ^

最佳答案

doc 中所述

Next Returns the next element in the list and advances the cursor position.

Previous Returns the previous element in the list and moves the cursor position backwards.

不明显的是,光标位置始终位于元素之间而不是元素上。文档中也对此进行了描述。

A ListIterator has no current element; its cursor position always lies between the element

知道了这一点,很明显,当您交替调用 next()previous() 时,您将获得相同的元素。

LinkedList lList = new LinkedList();
lList.add("1");
lList.add("2");
lList.add("3");
lList.add("4");
lList.add("5");
//lList look like [1,2,3,4,5]
ListIterator itr = lList.listIterator(); //create iterator at position before element 0

itr.next() // return the next element => so return "1"
           // And advance the cursor position => position between element 0 and element 1

itr.previous(); // return the previous element => so return "1"
           // And step back the cursor position => position before element 0

关于java - ListIterator 在交替调用 next() 和 previous() 时重复元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49446202/

相关文章:

java - 检测指纹扫描仪触摸或移动

java - 使用 hashmap 代替 arraylist

python - 在 Python 中读取 .csv 而不遍历整个文件?

.net - 当 Key 和 Value 相同时使用字典?

collections - 映射列表值以列出一个类轮

c++ - 通过外部评估调度将标准兼容性与不透明数据连接起来

c++ - 通过在 getter 方法上应用 begin() 获得的迭代器不允许访问指向列表的第一个元素

Java - 具有大量数据的字符串频率

java - Hibernate返回 "weird"连接

java - 如何在 Web 应用程序中维护每个项目类别的 'currently most popular' 项目列表?