title: Java Collections ArrayList, LinkedList cast Exception
body: <p>I found a bizarre case that I can't explain so far. </p>
  
  <pre><code> public static void main(String[] args) {
      LinkedList&lt;Integer&gt; a = new LinkedList&lt;&gt;();
      a.add(2);
      a.add(3);
      a.add(4);
  
      for (ArrayList ax : getBatches(a)){
          System.out.println(ax);
      }
  
  }
  
  private static ArrayList&lt;ArrayList&lt;Integer&gt;&gt; getBatches(List&lt;Integer&gt; optionIds) {
          return new ArrayList(Arrays.asList(optionIds));
  }
  </code></pre>
  
  <p>The result of execution is :</p>
  
  <blockquote>
    <p>Exception in thread "main" java.lang.ClassCastException: java.util.LinkedList cannot be cast to java.util.ArrayList</p>
  </blockquote>
  
  <p>The exception acours in for loop.</p>
  
  <p>The questions is : How? How a method with return type ArrayList&lt; ArrayList&lt; Integer>> can return a LinkedList?</p>