title: Deep copy of an ArrayList<Arraylist> in JAVA
body: <p>I checked other answers but I could not find a proper answer to my question.
  I want to create a copy of my <code>ArrayList&lt;ArrayList&gt;</code>, since I need the original one somewhere else. I used the <code>.clone()</code> method in different ways:</p>
  
  <pre><code>public class WordChecker {
  
      private ArrayList&lt;ArrayList&gt; copyOfList = new ArrayList&lt;ArrayList&gt;();
  
      public WordChecker(ArrayList&lt;ArrayList&gt; list) {
          for (int i = 0; i &lt; list.size(); i++)
              for (int j = 0; j &lt; 7; j++)
                  copyOfList = (ArrayList&lt;ArrayList&gt;) list.clone(); // without error
                  // copyOfList = list.clone();cannot convert from Object to
                  // ArrayList&lt;ArrayList&gt;
                  // copyOfList = list.get(i).clone();cannot convert from Object to
                  // ArrayList&lt;ArrayList&gt;
                  // copyOfList = list.get(i).get(j).clone();
      }
  </code></pre>
  
  <p>but still my main <code>ArrayList</code> changes as I work on its copy.
  Could anybody tell me how I should get a deep copy in this case?</p>
  
  <p><strong>Answer:</strong>
  I put the copying mechanism in my class constructor:</p>
  
  <pre><code>private ArrayList&lt;List&gt; checkedTags = new ArrayList&lt;List&gt;();
  public WordChecker(ArrayList&lt;ArrayList&gt; list)
    {
       for (ArrayList word: list) copyOfList.add((ArrayList) word.clone());
  
  }
  </code></pre>
  
  <p>the only problem is that this is not applicable to copy from ArrayList which made me to go through a for loop use .get() method.I feel they are basically the same at the end.</p>