Simple Java objects not deeply copying
<p>This question should be rather easy for any Java developer. I swear I looked it up after spending ~2 hours on it, but I can't really understand what's wrong with this code.</p>

<p>Basically, I am implementing Karger's minimum cuts algorithm. It requires me to keep merging nodes in a graph and then compute the number of crossing edges at the end (an int value). This algorithm must be repeated n times, always from the starting graph. My problem is that I am unable to create a deep copy of my Graph object, and I can't find the mistake.</p>

<p>I have cropped the code to just show the problem and no more, but I am still unable to figure out what's wrong. Here the code is.</p>

<p>Class Node:</p>

<pre><code>public class Node {
public Integer Data;


public Node() {
    Data = 0;
}

public Node(Node rhs) {
    Data = rhs.Data.intValue();
}

public Node(Integer rhs) {
    Data = rhs.intValue();
}

public void setNode(Integer rhs) {
    Data = rhs;
}
</code></pre>

<p>Class Graph:</p>

<pre><code>public class Graph {

public ArrayList&lt;ArrayList&lt;Node&gt;&gt; AdjList;
public ArrayList&lt;Node&gt; NodeSet; // This contains all the nodes

public Graph() {
    AdjList = new ArrayList&lt;ArrayList&lt;Node&gt;&gt;();
    NodeSet = new ArrayList&lt;Node&gt;();
}

public Graph(Graph G) {
    AdjList = new ArrayList&lt;ArrayList&lt;Node&gt;&gt;();
    for (ArrayList&lt;Node&gt; L : G.AdjList) {
        ArrayList&lt;Node&gt; Lcopy = new ArrayList&lt;Node&gt;();
        for (Node N : L) {
            Node copy = new Node(N);
            Lcopy.add(copy);
        }
        AdjList.add(L);
    }
}
    public void addNewAdjList(ArrayList&lt;Node&gt; NodeAdjList) {
    // Input is the adjacency list of a new node
    // The first element in the NodeAdjList is the node itself, the rest is the adj nodes
    AdjList.add(NodeAdjList);
}
public static void printAdjList(ArrayList&lt;Node&gt; Adjlist) {
    Node start = Adjlist.get(0);
    System.out.print(start.Data + " : ");
    for (int j=1; j &lt; Adjlist.size(); ++j) {
        System.out.print(Adjlist.get(j).Data + ", ");
    }
    System.out.print("\n");
}
</code></pre>

<p>Main:</p>

<pre><code>public class Main {

/**
 * @param args
 */
public static void main(String[] args) {
    Node Five = new Node(5);
    Node Seven = new Node(7);
    Node One = new Node(1);

    Graph G = new Graph();

    ArrayList&lt;Node&gt; L = new ArrayList&lt;Node&gt;();
    L.add(Five);
    L.add(Seven);
    L.add(One);

    G.addNewAdjList(L);

    Graph R = new Graph(G);
    R.AdjList.get(0).get(1).setNode(19); // Gets node #1 in the first adj list, i.e. 7

    Graph.printAdjList(G.AdjList.get(0));
    Graph.printAdjList(R.AdjList.get(0));

}
</code></pre>

<p>}</p>

<p>Output: </p>

<blockquote>
  <p>5 : 19, 1,</p>
  
  <p>5 : 19, 1, </p>
</blockquote>

<p>This kind of puzzles me to be honest. I understand that Java is pass by value only, but objects are always represented by their reference. As far as I understand, my copy constructor for G should always make a deep copy: I am moving through every adjacency list and then I am making a deep copy of the Node. I don't understand why invoking .setNode() on the copied object modifies also the original object (that has a different reference).</p>

<p>Previous answers like <a href="http://stackoverflow.com/questions/64036/how-do-you-make-a-deep-copy-of-an-object-in-java">1</a> seem to go the same direction I am going, what am I missing here? :S</p>
