title: Iterate parameterized List (after raw List type assignment)
body: <p>Language: Java<br>
  Compiler version: 1.6</p>
  
  <p>In the below code, am trying to do the following:</p>
  
  <ol>
  <li>create a <code>List&lt;String&gt;</code></li>
  <li>add a <code>String</code></li>
  <li>assign <code>List&lt;String&gt;</code> to raw <code>List</code></li>
  <li>create a <code>List&lt;Integer&gt;</code></li>
  <li>assign the raw <code>List</code> to <code>List&lt;Integer&gt;</code></li>
  <li>add an <code>Integer</code></li>
  <li>retrieve the value using <code>get()</code> @ indexes 1 &amp; 2 and print them.</li>
  </ol>
  
  <p>All statements are compiling (with warnings) and run fine. </p>
  
  <p>But if I try to loop through the <code>List&lt;Integer&gt;</code> using a <code>for</code> loop, I am getting a <code>ClassCastException</code>. I am just wondering why its allowed me to use <code>list.get()</code> method but not allowing me to iterate over it?</p>
  
  <p><strong>Output:</strong> <em>(if I run with un-commented for loop) abcd 200</em> </p>
  
  <pre><code>Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot  be cast to java.lang.Integer
          at genericsamples.CheckRawTypeAdd.main(CheckRawTypeAdd.java:26)
  </code></pre>
  
  <p>Here is my code</p>
  
  <pre><code>import java.util.*;
  import java.io.*;
  class CheckRawTypeAdd
  {
      public static void main(String[] sr)
      {   
          List&lt;String&gt; list_str = new ArrayList&lt;String&gt;();
          list_str.add("abcd");
          List&lt;Integer&gt; list_int = new ArrayList&lt;Integer&gt;();  
          List list_raw; 
          list_raw=list_str;
          list_int=list_raw;
          list_int.add(200);
          Object o1 = list_int.get(0);
          Object o2 = list_int.get(1);        
          System.out.println(o1);
          System.out.println(o2);
          //for(Object o : list_int)
          //{
          //  System.out.println("o value is"+o);
          //}
      }
  }
  </code></pre>