2d arraylists in java (why are the collections the same eventually)
<p>I have a 2D <code>ArrayList</code>. The <code>ArrayList</code> contains 10 <code>ArrayList</code>s. I tried the following code:</p>

<p>This is the main 2D <code>ArrayList</code>. Inside the main <code>ArrayList</code> there are10 <code>ArrayList</code>s:</p>

<pre><code> ArrayList&lt;ArrayList&lt;Items&gt;&gt; arrayList = new ArrayList&lt;ArrayList&lt;Items&gt;&gt;();
</code></pre>

<p>Here I tried to create a copy of one of the <code>ArrayList</code> (<code>selectedRow</code> is just a number which says which <code>ArrayList</code> I get)</p>

<pre><code>ArrayList&lt;Items&gt; newList = new ArrayList&lt;Items&gt;(arrayList.get(selectedRow));
</code></pre>

<p>After that I create another <code>ArrayList</code>:</p>

<pre><code>   ArrayList&lt;Items&gt; changeList = new ArrayList&lt;Items&gt;(it.returnTheNewArrayList(newList,randomItem));
</code></pre>

<p>Then in another class I created this method. The purpose of this method is to change an attribute of one of the objects.</p>

<pre><code>public  ArrayList&lt;Items&gt; returnTheNewArrayList(ArrayList&lt;Items&gt; a,int item){

  int randomBin = r.nextInt(50);

    for(Items i:a){

        if(item==i.itemIds()){

            while(randomBin==i.bins()){
                randomBin = r.nextInt(50);

            }
            i.setBin(randomBin);

        }

    }
return a;

}
</code></pre>

<p>Finally, I set the new ArrayList in the 2D ArrayList</p>

<pre><code>arrayList.set(whichList, changeList);
</code></pre>

<p>This is the procedure. I have find out, that when I run this procedure, both <code>newList</code> and <code>changeList</code> are the same. In both of these <code>ArrayLists</code> I'm saving the change I did in the method <code>returnTheNewArrayList</code> (I found out it with the debugging). But I want to change only one (the <code>changeList</code>). </p>

<p>What did I do wrong?</p>
