title: How make a List of ArrayList in Java?
body: <p>I'm trying to make a list of lists, but I noticed that my list ends up empty at the end of the loop.</p>
  
  <pre><code>ArrayList&lt;ArrayList&lt;Integer&gt;&gt; list_of_lists = new ArrayList&lt;ArrayList&lt;Integer&gt;&gt;();
      List&lt;Integer&gt; list = new ArrayList&lt;Integer&gt;();          
      for(int x = 0; x &lt; 5; x++) {
          for(int y = 0; y &lt; 2; y++) {                
              list.add(x);
              list.add(y);                
              list_of_lists.add((ArrayList&lt;Integer&gt;) list);
              System.out.println(list_of_lists);
              list.clear();
          }
      }
      System.out.println(list_of_lists);
  </code></pre>
  
  <p><strong>But, in console the result is:</strong></p>
  
  <pre><code>[[0, 0]]
  [[0, 1], [0, 1]]
  [[1, 0], [1, 0], [1, 0]]
  [[1, 1], [1, 1], [1, 1], [1, 1]]
  [[2, 0], [2, 0], [2, 0], [2, 0], [2, 0]]
  [[2, 1], [2, 1], [2, 1], [2, 1], [2, 1], [2, 1]]
  [[3, 0], [3, 0], [3, 0], [3, 0], [3, 0], [3, 0], [3, 0]]
  [[3, 1], [3, 1], [3, 1], [3, 1], [3, 1], [3, 1], [3, 1], [3, 1]]
  [[4, 0], [4, 0], [4, 0], [4, 0], [4, 0], [4, 0], [4, 0], [4, 0], [4, 0]]
  [[4, 1], [4, 1], [4, 1], [4, 1], [4, 1], [4, 1], [4, 1], [4, 1], [4, 1], [4, 1]]
  [[], [], [], [], [], [], [], [], [], []]
  </code></pre>
  
  <p><strong>Why, the final is a empy list insteed of :</strong></p>
  
  <pre><code>[[0, 0], [0, 1], [1, 0], [1, 1], [2, 0], [2, 1], [3, 0], [3, 1], [4, 0], [4, 1]]
  </code></pre>
  
  <p><strong>It's with each other instead of two separate lists. My code was linking one list to another</strong></p>