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