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

Viewed 407

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("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;
    }
}

OUTPUT:

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)
2 Answers

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("blue")){
         iterator.add(new Color("yellow"));
     }
}

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

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)
}

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.

Related