Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何获取linkedlist上的下一个对象?_Java_Linked List_Listiterator - Fatal编程技术网

Java 如何获取linkedlist上的下一个对象?

Java 如何获取linkedlist上的下一个对象?,java,linked-list,listiterator,Java,Linked List,Listiterator,我在Java中有一个对象的LinkedList,我想遍历它,当我在那里找到一个特定的对象时,我想获取下一个对象(第一个对象旁边的对象) 我认为这会解决这个问题: listIterator = items.listIterator(); while (listIterator.hasNext() && listIterator.previous().getCode().equals(search.getCurrentCode())) { item = listIterat

我在
Java
中有一个对象的
LinkedList
,我想遍历它,当我在那里找到一个特定的对象时,我想获取下一个对象(第一个对象旁边的对象)

我认为这会解决这个问题:

listIterator = items.listIterator();
while (listIterator.hasNext() && listIterator.previous().getCode().equals(search.getCurrentCode())) {

    item = listIterator.next();
    result.setCurrentCode(item.getCode());
    break;
}
但我得到了一个错误:

java.util.NoSuchElementException: null
我认为这是因为使用了
.previous
,但我不知道如何正确处理它,那么我该如何解决它呢?我使用的是
previous
,但我想要的是使用当前元素-我认为这是由
处理的。previous
,但显然不是。

试试这个:

listIterator = items.listIterator();
// listIterator.previous() does not exist in first iteration
while (listIterator.hasNext()) {
// you can compare previous value if it exist
if(listIterator.hasPrevious() && listIterator.previous().getCode().equals(search.getCurrentCode())){
    item = listIterator.next();
    result.setCurrentCode(item.getCode());
    break;
}
}
试试这个:

listIterator = items.listIterator();
// listIterator.previous() does not exist in first iteration
while (listIterator.hasNext()) {
// you can compare previous value if it exist
if(listIterator.hasPrevious() && listIterator.previous().getCode().equals(search.getCurrentCode())){
    item = listIterator.next();
    result.setCurrentCode(item.getCode());
    break;
}
}

您当前的代码失败,因为您正在调用previous,甚至在开始迭代项之前。迭代使用
listIterator.next()完成呼叫。
您可以尝试下面的代码

while (listIterator.hasNext()){
   // iterate always
   item = listIterator.next();
   // if found and an element still exist
   if(item.getCode().equals(search.getCurrentCode() && listIterator.hasNext()){
      // get the next element
      item = listIterator.next();
      result.setCurrentCode(item.getCode());
      break;
   }
}

您当前的代码失败,因为您正在调用previous,甚至在开始迭代项之前。迭代使用
listIterator.next()完成呼叫。
您可以尝试下面的代码

while (listIterator.hasNext()){
   // iterate always
   item = listIterator.next();
   // if found and an element still exist
   if(item.getCode().equals(search.getCurrentCode() && listIterator.hasNext()){
      // get the next element
      item = listIterator.next();
      result.setCurrentCode(item.getCode());
      break;
   }
}

首先,我建议使用
ArrayList
而不是
LinkedList
,因为前者几乎总是更好的选择。了解更多信息

您完全可以通过典型的for循环或增强的for循环来实现这一点,但我想说明一种从JDK9开始称为
dropWhile
的新方法,它可以帮助您实现这一需求:

假设您已经准备好了
ArrayList
。你可以简单地做:

myList.stream()
      .dropWhile(c -> !c.getCode().equals(search.getCurrentCode()))
      .skip(1) // get the object to the right of the matched item
      .findFirst() // retrieve this object
      .ifPresent(s -> result.setCurrentCode(s.getCode())); // apply the logic based on the found object

首先,我建议使用
ArrayList
而不是
LinkedList
,因为前者几乎总是更好的选择。了解更多信息

您完全可以通过典型的for循环或增强的for循环来实现这一点,但我想说明一种从JDK9开始称为
dropWhile
的新方法,它可以帮助您实现这一需求:

假设您已经准备好了
ArrayList
。你可以简单地做:

myList.stream()
      .dropWhile(c -> !c.getCode().equals(search.getCurrentCode()))
      .skip(1) // get the object to the right of the matched item
      .findFirst() // retrieve this object
      .ifPresent(s -> result.setCurrentCode(s.getCode())); // apply the logic based on the found object

这个问题有点难理解,至少对我来说是这样。你能举一个这样一个列表的例子吗?你使用
链接列表
而不是
数组列表
的具体原因是什么?。几乎在所有情况下,人们都倾向于后者。这个问题有点难以理解,至少对我来说是这样。你能举一个这样一个列表的例子吗?你使用
链接列表
而不是
数组列表
的具体原因是什么?。在几乎所有情况下,人们都会倾向于后者。