【问题标题】:ListIterator previous() and next() resultListIterator previous() 和 next() 结果
【发布时间】:2019-07-12 21:52:00
【问题描述】:

结果是

A B C D

D C C2 B2 B A

为什么没有结果

A B B2 C D

D C C2 B2 B A in first while ?

如果 word.equals"B",我做了 li.add("B2")。只是 next() 和 previous() 之间的区别吗?我想知道答案。

public static void main(String[] args) {

    List<String> list = Arrays.asList("A", "B", "C", "D");
    list = new ArrayList<>(list);
    ListIterator<String> li = list.listIterator();
    String word;

    while (li.hasNext()) {
        word = li.next();
        System.out.print(word + '\t');
        if (word.equals("B"))
           li.add("B2");
    }

    System.out.println();

    while (li.hasPrevious()) {
        word = li.previous();
        System.out.print(word + '\t');
        if (word.equals("C"))
            li.add("C2");
    }
}

【问题讨论】:

    标签: java listiterator


    【解决方案1】:

    我认为您的问题的答案在这里 - https://docs.oracle.com/javase/7/docs/api/java/util/ListIterator.html#add(E)

    该元素被插入到 next() 将返回的元素(如果有)之前,以及在 previous() 将返回的元素(如果有)之后。

    新元素插入到隐式光标之前:后续调用 next 将不受影响,后续调用 previous 将返回新元素。

    在第一次循环中,当 word 等于“B”时,隐式光标在“B”和“C”之间,根据文档,新元素将被添加到它之前。

    A    B       C     D
           ^   ^ 
          B2  cursor
     
    

    【讨论】:

      【解决方案2】:

      ListIterator.add(E):

      ...该元素被插入紧接在将由 next() 返回的元素之前...

      所以在您的第一次迭代中,添加的B2 不会成为列表中的下一个。

      【讨论】:

      • 但是在第二个时间里,C2成为列表中的下一个
      【解决方案3】:

      https://docs.oracle.com/javase/7/docs/api/java/util/ListIterator.html

      原因是当您向迭代器添加元素时,这不会更改next() 元素,而只是更改previous()。因此,当您添加“B2”和“C2”时,它只会被previous() 调用接收。这就是为什么第一次迭代没有选择 B2 和 C2 而第​​二次向后迭代选择了它们。

      【讨论】:

        【解决方案4】:

        ListIterator#add 的 JavaDoc 指定行为:

         * Inserts the specified element into the list (optional operation).
         * The element is inserted immediately before the element that
         * would be returned by {@link #next}, if any, and after the element
         * that would be returned by {@link #previous}, if any.  (If the
         * list contains no elements, the new element becomes the sole element
         * on the list.)  The new element is inserted before the implicit
         * cursor: a subsequent call to {@code next} would be unaffected, and a
         * subsequent call to {@code previous} would return the new element.
        

        混乱在哪里?

        【讨论】:

          【解决方案5】:

          您可以通过将打印更改为使用 nextIndex 来查看列表更改,如下所示

          System.out.print(list.get(li.nextIndex()) + "["+ li.nextIndex()+ "]" +'\t');
          
          
          while (li.hasNext()) {
              System.out.print(list.get(li.nextIndex()) + "["+ li.nextIndex()+ "]" +'\t');
              word = li.next();
              if (word.equals("B")) {
                   li.add("B2");
              }
            }
          
          System.out.println();
          
          while (li.hasPrevious()) {
              word = li.previous();
              System.out.print(list.get(li.nextIndex()) + "["+ li.nextIndex()+ "]" +'\t');
              if (word.equals("C"))
                  li.add("C2");
              }
          }
          

          这个输出

          A[0]    B[1]    C[3]    D[4]     
          D[4]    C[3]    C2[3]   B2[2]   B[1]    A[0]
          

          【讨论】:

          • 感谢回答。使用 nextIndex 可以帮助我了解更多。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-09-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-04-14
          • 2017-04-28
          • 1970-01-01
          相关资源
          最近更新 更多