title: How to reclaim the generic type you declared an arraylist to be in Java?
body: <p>I was just playing around and a thought came to my mind and I decided I want to try it:
  Make an ArrayList that holds more ArrayLists.</p>
  
  <p>For example, I created an ArrayList called intList that holds ints, then filled it with values. After that I did a stringList one and filled it too. Then I made an ArrayList that holds other ArrayLists called aList and added intList and stringList to it.</p>
  
  <p>Now the problem I faced was if I was retrieving objects from aList, it would not recognize if the generic type was int or string.</p>
  
  <p>Here is the code I tried:</p>
  
  <pre><code>    import java.util.*;
  
  public class Practice {
      public static void main(String[] args) {
  
  List&lt;ArrayList&gt; list = new ArrayList&lt;ArrayList&gt;();
  
       ArrayList&lt;int&gt; intList = new ArrayList&lt;int&gt;();
       intList.add(1);
       intList.add(2);
       intList.add(3);
  
       ArrayList&lt;String&gt; stringList = new ArrayList&lt;String&gt;();
       stringList.add("One");
       stringList.add("Two");
       stringList.add("Three");
  
       list.add(intList);
       list.add(stringList);
  
       for(ArrayList lst : list) {
        for(ArrayList lt : lst) {
         System.out.println(lt);
        }
       }
      } 
  }
  </code></pre>