【问题标题】:Java listIterator() is giving me weird outputs with .next() and .prev()Java listIterator() 用 .next() 和 .prev() 给我奇怪的输出
【发布时间】:2020-07-23 21:13:22
【问题描述】:

我一直在从事一个项目,我从头开始使用单独的“节点类”实现了一个(双向链接列表)。

然后我已经到了需要对“节点链接列表”进行排序的地步。由于我从头开始实现了我的链表,所以为了对其进行排序,我也必须从头开始为我的链表实现“合并排序”,这有点费时。

所以我考虑使用 java.util 中的“Java Linked List”和 listIterator(),然后使用 Collections.sort() 对我的 LinkedList 进行排序,但是它的 next() 和 previous() 给了我一些意想不到的奇怪输出与我使用 (.next) & (.prev) 直接访问节点的 LinkedList 时相比。例如,假设:

node1.time = 7;
node2.time = 8;
node3.time = 9;
node4.time = 10;

LinkedList<Node> nodeList = new LinkedList<Node>():
nodeList.add(node1); nodeList.add(node2); nodeList.add(node3); nodeList.add(node4);

void testFunction() {

  ListIterator<Node> nodesIterator = nodeList.listIterator();

  Node current;

  for (int i = 0; i < 2; i++) {
    current = nodesIterator.next();
    System.out.println("current = " + current.time);
  }
  System.out.println("outside of loop:"); 

  System.out.println("move current backward:");
  current = nodesIterator.previous();
  System.out.println("current = " + current.time);

  System.out.println("move current forward:");
  current = nodesIterator.next();
  System.out.println("current = " + current.time);

  System.out.println("Passing nodesIterator into testFunction2():");
  testFunction2(nodesIterator);   
}


void testFunction2(ListIterator<Node> nodesIterator) {

  System.out.println("inside testFunction2():");

  Node current = nodesIterator.next();
  System.out.println("current = " + current.time);

  System.out.println("move current backward:");
  current = nodesIterator.previous();
  System.out.println("current = " + current.time);

  System.out.println("move current backward again:");
  current = nodesIterator.previous();
  System.out.println("current = " + current.time);
}

输出:

current = 7
current = 8

outside of loop:

move current backward:
current = 8
 // -> current is suppose to be 7 if previous current inside the loop was 8?

move current forward:
current = 8
 // -> current is suppose to be 9 if previous current = 8?

Passing nodesIterator into testFunction2():

inside testFunction2():
current = 9
 // -> guess it's correct since previous current = 8?

move current backward:
current = 9
 // -> suppose to give me 8 since previous current = 9?

move current backward again:
current = 8
 // -> now it actually moved backward!

Java 的 next() 和 prev() 是怎么回事?我从头开始实现的链接列表永远不会给我这些问题,而且通过直接访问 (.next) 和 (.prev) 将节点传递给其他函数进行遍历更简单,因为我可以传入 (node.next) 或 ( node.prev) 到其他函数,而无需传入 listIterator() 引用来链接我的节点列表。

我是否应该从头开始使用我的链接列表,只编写“合并排序”代码

【问题讨论】:

标签: java doubly-linked-list next


【解决方案1】:

ListIteratordocumentation 解释了这个问题。基本上“当前”位置不是单个节点,它位于两个节点之间。具体来说,它位于调用prev() 或调用next() 时将返回的节点之间。例如,在您对 next() 的前两次调用之后,您的迭代器如下所示:

7 -&gt; 8 *-&gt;* 9 -&gt; 10 电流在89 之间。

调用prev() 将返回上一个节点,即8。然后,迭代器将如下所示:

7 *-&gt;* 8 -&gt; 9 -&gt; 10 电流在78 之间。

接下来,再次调用next() 将返回8,以此类推。这是设计使然,您在使用ListIterator 遍历时必须考虑到这一点。

【讨论】:

  • 感谢您的直截了当的回答。我最终为我的链接列表实现了合并排序,因为 Java listIterator() 在遍历和“返回最后一个节点”时效率不高我必须“重新链接我的节点引用”到“ListIterator 引用”,而不是直接访问 .next & .prev
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-29
  • 1970-01-01
  • 1970-01-01
  • 2019-07-12
  • 2014-01-21
  • 2014-10-29
  • 1970-01-01
相关资源
最近更新 更多