首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Java listIterator()用.next()和.prev()给出了奇怪的输出

Java listIterator()用.next()和.prev()给出了奇怪的输出
EN

Stack Overflow用户
提问于 2020-04-11 11:41:12
回答 1查看 94关注 0票数 0

我一直在做一个项目,在这个项目中我用一个单独的"Node类“从头开始实现了一个(双向链表)。

然后,我需要对我的“节点链表”进行排序。因为我从头开始实现我的链表,所以为了对它进行排序,我也必须从头开始为我的链表实现“合并排序”,这样做有点耗时。

因此,我考虑使用java.util中的"Java Linked List“和listIterator(),然后使用Collections.sort()对LinkedList进行排序,但是当我使用(.next) & (.prev)遍历节点的LinkedList时,它的next()和previous()给出了一些意想不到的奇怪输出。例如,假设:

代码语言:javascript
运行
复制
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);
}

输出:

代码语言:javascript
运行
复制
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()引用来链接我的节点列表。

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

EN

回答 1

Stack Overflow用户

发布于 2020-04-11 11:58:33

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

7 -> 8 *->* 9 -> 10电流在89之间。

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

7 *->* 8 -> 9 -> 10电流在78之间。

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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61151842

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档