title: List<List<Integer>> ArrayList initialization does not work with new operator Java
body: <p>I have been struggling on this problem for two days. This problem comes from some problem I am doing. Basically, when I use </p>
  
  <pre><code>List&lt;List&lt;Integer&gt;&gt; temp = new ArrayList&lt;&gt;(result);
  </code></pre>
  
  <p>to create a new ArrayList copy of result, the result will change when I try to change temp within an advanced for loop. For example, </p>
  
  <pre><code>List&lt;List&lt;Integer&gt;&gt; result = new ArrayList&lt;&gt;();
  result.add(new ArrayList&lt;&gt;());
  List&lt;List&lt;Integer&gt;&gt; temp = new ArrayList&lt;&gt;(result);
  int j = 0;
  for (List&lt;Integer&gt; list: temp) {
      list.add(x[j]);
      j ++;
  }
  </code></pre>
  
  <p>I did nothing to the result inside the loop but the result ends up with [[1]], which is the same as temp.</p>
  
  <p>Why is this happening? Thanks a lot.</p>
  
  <p>Update: thank everybody for answering my question. I learn the shallow copy was the reason. However, I still run into a similar problem. The result is updated while I try to change the temp in the following code:</p>
  
  <pre><code>List&lt;List&lt;Integer&gt;&gt; result = new ArrayList&lt;&gt;();
  result.add(new ArrayList&lt;&gt;());
  List&lt;List&lt;Integer&gt;&gt; temp = new ArrayList&lt;&gt;();
  for (List&lt;Integer&gt; list: result) {
      list.add(10000);
      temp.add(new ArrayList(list));
  }
  </code></pre>
  
  <p>I do not know why the result turns out to be [[10000]] as well as the temp. Is there anything wrong with the add method like temp.add(new ArrayList(list))?</p>