title: LinkedLists: infinite loop
body: <p>Why does the following java code result in an infinite loop?</p>
  
  <pre><code>import java.util.LinkedList;
  import java.util.Random;
  
  public class Main {
  
  public static void main(String[] args) {
      int n = 3;
  
      Random rand = new Random();
  
      LinkedList&lt;Integer&gt; fields = new LinkedList&lt;Integer&gt;();
  
      for (int i = 0; i &lt; n*n; i++) {
          fields.add(i);
      }
  
      while (fields.size() &gt; 0) {
  
          // Choose Field
          int f = rand.nextInt(fields.size());
  
          fields.remove((Integer) f);
          System.out.println(fields.size());
      }
  }
     }
  </code></pre>