为什么在使用listIterator.add()时会出现NoSuchElementException错误?

huangapple go评论71阅读模式
英文:

Why do I get noSuchElementException when using listIterator.add()?

问题

没有进一步的延迟,我将立即发布我的代码。它非常简单,所以你不应该在理解它方面遇到任何问题。我的想法是在找到“blue”后,使用listIterator在其后面“添加”颜色。显然这里有些问题。

public class Main {
    public static void main(String[] args) {
        Color c1 = new Color("red");
        Color c2 = new Color("blue");
        Color c3 = new Color("green");

        List<Color> list = new ArrayList<>();
        list.add(c1);
        list.add(c2);
        list.add(c3);

        ListIterator<Color> iterator = list.listIterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
            if (iterator.next().tone.equals("blue")){
                iterator.add(new Color("yellow"));
            }
        }

    }
}

class Color {
    String tone;

    public Color(String tone) {
        this.tone = tone;
    }

    @Override
    public String toString() {
        return tone;
    }
}

输出:

red
green
Exception in thread "main" java.util.NoSuchElementException
	at java.base/java.util.ArrayList$Itr.next(ArrayList.java:999)
	at Main.main(Main.java:21)
英文:

Without further delay, I will post my code straight away. It is very simple so you shouldn't find any problems understanding it. My idea is to add color after it found "blue" using listIterator. Apparently there is something wrong here.

public class Main {
    public static void main(String[] args) {
        Color c1 = new Color(&quot;red&quot;);
        Color c2 = new Color(&quot;blue&quot;);
        Color c3 = new Color(&quot;green&quot;);

        List&lt;Color&gt; list = new ArrayList&lt;&gt;();
        list.add(c1);
        list.add(c2);
        list.add(c3);

        ListIterator&lt;Color&gt; iterator = list.listIterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
            if (iterator.next().tone.equals(&quot;blue&quot;)){
                iterator.add(new Color(&quot;yellow&quot;));
            }
        }

    }
}

class Color {
    String tone;

    public Color(String tone) {
        this.tone = tone;
    }

    @Override
    public String toString() {
        return tone;
    }
}

OUTPUT:

red
green
Exception in thread &quot;main&quot; java.util.NoSuchElementException
	at java.base/java.util.ArrayList$Itr.next(ArrayList.java:999)
	at Main.main(Main.java:21)

答案1

得分: 2

你调用hasNext()一次,可以调用next()两次。调用一次并保存,以便能够多次使用。

while (iterator.hasNext()){
     Color current = iterator.next();
     System.out.println(current);
     if (current.tone.equals("blue")){
         iterator.add(new Color("yellow"));
     }
}
英文:

You call hasNext() once for 2 calls of next(). Call it once and save it to be able to use multiple times

while (iterator.hasNext()){
     Color current = iterator.next();
     System.out.println(current);
     if (current.tone.equals(&quot;blue&quot;)){
         iterator.add(new Color(&quot;yellow&quot;));
     }
}

答案2

得分: 1

我知道答案已经提供了,但是如果有帮助的话就发出来:
请参考以下代码:-

    public static void main(String[] args) {
        Color c1 = new Color("red");
        Color c2 = new Color("blue");
        Color c3 = new Color("green");

        List<Color> list = new ArrayList<>();
        list.add(c1);
        list.add(c2);
        list.add(c3);

        ListIterator<Color> iterator = list.listIterator();
        System.out.println(iterator.next());//-->red
        System.out.println(iterator.next());//-->blue
        System.out.println(iterator.next());//-->green
        System.out.println(iterator.next());//-->java.util.NoSuchElementException (iterator is moved forward but list is exhausted)
    }

所以基本上 `iterator.next()` 会定位到可迭代集合中的元素
因此在您的 `while` 循环中多次调用 `iterator.next()` 会每次将光标移动到下一个值因此最好将 `iterator.next()` 存储在循环内的变量中
英文:

I know the answer is provided but posting if it helps
refer below code:-

public static void main(String[] args) {
    Color c1 = new Color(&quot;red&quot;);
    Color c2 = new Color(&quot;blue&quot;);
    Color c3 = new Color(&quot;green&quot;);

    List&lt;Color&gt; list = new ArrayList&lt;&gt;();
    list.add(c1);
    list.add(c2);
    list.add(c3);

    ListIterator&lt;Color&gt; iterator = list.listIterator();
    System.out.println(iterator.next());//--&gt;red
    System.out.println(iterator.next());//--&gt;blue
    System.out.println(iterator.next());//--&gt;green
    System.out.println(iterator.next());//--&gt;java.util.NoSuchElementException (iterator is moved forward but list is exhausted)
}

so basically iterator.next() locates the element in the iterable collection.
hence calling iterator.next() multiple time in ur while loop will shift the cursor eveytime to next value , hence its better to store iterator.next() into a variable inside the loop.

huangapple
  • 本文由 发表于 2020年9月28日 04:03:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/64092798.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定