Java LinkedList NoTouchElementException

Java LinkedList NoTouchElementException,java,exception,linked-list,Java,Exception,Linked List,我有以下评估VRP路由成本的方法,但它抛出了java.util.NoSuchElementException 起初我以为问题出在第一次迭代中,其中迭代器.next()为null,这就是为什么我添加了布尔值firstIteration,但问题仍然存在 private void evaluateRouteCost () { ListIterator<VRPNode> iterator = this.routeList.listIterator(); boolean f

我有以下评估VRP路由成本的方法,但它抛出了java.util.NoSuchElementException

起初我以为问题出在第一次迭代中,其中迭代器.next()为null,这就是为什么我添加了布尔值firstIteration,但问题仍然存在

private void evaluateRouteCost () {

    ListIterator<VRPNode> iterator = this.routeList.listIterator();

    boolean firstIteration=true;

    while (iterator.hasNext()) {

        if (firstIteration) {
            firstIteration=false;
        }

        else {
            this.routeCost += vrp.distance(iterator.previous(), iterator.next()); 
        }

    }
private void evaluteroutecost(){
ListIterator迭代器=this.routeList.ListIterator();
布尔第一迭代=真;
while(iterator.hasNext()){
if(第一次迭代){
第一次迭代=假;
}
否则{
this.routeCost+=vrp.distance(iterator.previous(),iterator.next());
}
}

请注意,routeList是一个LinkedList。

如果
firstIteration
为true,则仍然需要调用
迭代器.next()
(否则,在第二次迭代中,您仍然位于第一个元素上)

不过,我会写得稍微不同:

ListIterator<VRPNode> iterator = this.routeList.listIterator();

while (iterator.hasNext()) {

    VRPNode current = iterator.next();

    if (iterator.hasPrevious())
        this.routeCost += vrp.distance(iterator.previous(), current); 

}
ListIterator迭代器=this.routeList.ListIterator();
while(iterator.hasNext()){
VRPNode current=iterator.next();
if(迭代器.hasPrevious())
this.routeCost+=vrp.distance(迭代器.previous(),当前);
}

您正在呼叫。上一个在第一个节点上。另外:您可以将其简化为:

private void evaluateRouteCost () {

    ListIterator<VRPNode> iterator = this.routeList.listIterator();

    boolean firstIteration=true;

    if (iterator.hasNext()) {
      iterator.next();
    }    

    while (iterator.hasNext()) {
        this.routeCost += vrp.distance(iterator.previous(), iterator.next()); 
    }
private void evaluteroutecost(){
ListIterator迭代器=this.routeList.ListIterator();
布尔第一迭代=真;
if(iterator.hasNext()){
iterator.next();
}    
while(iterator.hasNext()){
this.routeCost+=vrp.distance(iterator.previous(),iterator.next());
}

}

我用一种简单的方法修复了它:

public void evaluateRouteCost () { //http://bit.ly/t76G1Z

    ListIterator<VRPNode> iterator = this.routeList.listIterator();

    while(iterator.hasNext()) {

        int currentId, nextId;

        currentId=iterator.next().getId();

        try {
            nextId=iterator.next().getId();
        }
        catch (NoSuchElementException e) {
            // when reaches the last element
            break;
        }

        this.routeCost += vrp.distance(currentId, nextId);

        iterator.previous();    
    }

}
public void evaluteroutecost(){//http://bit.ly/t76G1Z
ListIterator迭代器=this.routeList.ListIterator();
while(iterator.hasNext()){
int currentId,nextId;
currentId=iterator.next().getId();
试一试{
nextId=iterator.next().getId();
}
捕获(无接触元素例外e){
//何时到达最后一个元素
打破
}
this.routeCost+=车辆路径距离(currentId,nextId);
迭代器.previous();
}
}

您还可以添加一些
System.out.println()
s,让您看到发生了什么。您很容易发现是witch元素导致了问题。您必须在循环终止之前添加另一个迭代器.next()