title: Deep copy of an object Java
body: <p>I am trying to clone an object of MyGraph and I want it to be a deep copy so the arraylists inside the object are also cloned. Right now I have:</p>
  
  <pre><code>public static MyGraph deepCopy(MyGraph G){
      MyGraph Copy = (MyGraph) G.clone();
  
      Copy.VertexG = (ArrayList&lt;Integer&gt;) G.VertexG.clone();
      Copy.EdgeG = (ArrayList&lt;String&gt;) G.EdgeG.clone();
  
      return Copy;
  }
  </code></pre>
  
  <p>This returns an error when it tries to clone the arraylist. I am not sure if this is the right way to add the arraylists to the object.</p>