• Martin Thoma
  • Home
  • Categories
  • Tags
  • Archives
  • Support me

Java Puzzle #11: Change argument of foreach

Contents

  • Java Puzzle #11: Change argument of foreach
    • Answer

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)

Published

Okt 14, 2012
by Martin Thoma

Category

Code

Tags

  • Java 36
  • Programming 52
  • puzzle 22

Contact

  • Martin Thoma - A blog about Code, the Web and Cyberculture
  • E-mail subscription
  • RSS-Feed
  • Privacy/Datenschutzerklärung
  • Impressum
  • Powered by Pelican. Theme: Elegant by Talha Mansoor