Java Puzzle #11: Change argument of foreach

· by

What is the output of the following HelloWorld.java?

import java.util.LinkedList;

public class HelloWorld {
    public static void main(String[] args) {
        LinkedList<Integer> list = new LinkedList<Integer>();
        list.add(1);
        list.add(2);
        list.add(3);
        int i = 0;
        for (Integer el : list) {
            System.out.println(el);
            list.add(el);
            i++;
            if (i > 20) {
                break;
            }
        }
    }
}

. . . . . . . . . . . . . . . . . . . . . . . .

Answer

1
Exception in thread "main" java.util.ConcurrentModificationException
    at java.util.LinkedList$ListItr.checkForComodification(LinkedList.java:761)
    at java.util.LinkedList$ListItr.next(LinkedList.java:696)
    at HelloWorld.main(HelloWorld.java:10)