title: Clone the complete ArrayList including containing objects into other ArrayList Java
body: <p>Hi I want to copy one ArrayList to another. Able to crate new list but whenever doing change in the object of second list, it is reflecting to first list.</p>
  
  <p><strong>Student POJO class:-</strong></p>
  
  <pre><code>public class Student {
  private int serialNo;
  private int rollNo;
  private String Name;
  
  public Student(int serialNo, int rollNo, String name) {
  super();
  this.serialNo = serialNo;
  this.rollNo = rollNo;
  Name = name;
  }
  
  public int getSerialNo() {
  return serialNo;
  }
  
  public void setSerialNo(int serialNo) {
  this.serialNo = serialNo;
  }
  
  public int getRollNo() {
  return rollNo;
  }
  
  public void setRollNo(int rollNo) {
  this.rollNo = rollNo;
  }
  
  public String getName() {
  return Name;
  }
  
  public void setName(String name) {
  Name = name;
  }
  
  @Override
  public String toString() {
      return "Student:: " 
  + "serialNo: " + serialNo 
  + ", serialNoDisplay:" + rollNo 
  + ", Name: " + Name;
  }
  }
  </code></pre>
  
  <p><strong>InSide Main Method:-</strong></p>
  
  <pre><code>    ArrayList&lt;Student&gt; arrayListOrignal = new ArrayList&lt;Student&gt;();
  
      arrayListOrignal.add(new Student(1, 100, "name1"));
      arrayListOrignal.add(new Student(2, 200, "name2"));
  
      System.out.println("******** arrayListOrignal **********");
      for (Student student : arrayListOrignal) {
          System.out.println(student.toString());
      }
      System.out.println("**************************\n");
  
  //METHOD 1
  
      ArrayList&lt;Student&gt; arrayListDuplicate = new ArrayList&lt;Student&gt;(arrayListOrignal);
  
      arrayListDuplicate.get(1).setRollNo(754);
      arrayListDuplicate.get(1).setName("name754");
  
      System.out.println("******** arrayListDuplicate **********");
      for (Student student : arrayListDuplicate) {
          System.out.println(student.toString());
      }
      System.out.println("**************************\n");
  
      System.out.println("******** arrayListOrignal **********");
      for (Student student : arrayListOrignal) {
          System.out.println(student.toString());
      }
      System.out.println("**************************\n");
  
  //METHOD 2
  
      ArrayList&lt;Student&gt; arrayListDuplicate2 = (ArrayList&lt;Student&gt;) arrayListOrignal.clone();
  
      arrayListDuplicate2.get(1).setRollNo(754);;
      arrayListDuplicate2.get(1).setName("name754");
  
      System.out.println("******** arrayListDuplicate **********");
      for (Student student : arrayListDuplicate2) {
          System.out.println(student.toString());
      }
      System.out.println("**************************\n");
  
      System.out.println("******** arrayListOrignal **********");
      for (Student student : arrayListOrignal) {
          System.out.println(student.toString());
      }
      System.out.println("**************************\n");
  
  //METHOD 3
  
      ArrayList&lt;Student&gt; arrayListDuplicate3 = new ArrayList&lt;Student&gt;();
      arrayListDuplicate3.addAll(arrayListOrignal);
  
      arrayListDuplicate3.get(1).setRollNo(754);
      arrayListDuplicate3.get(1).setName("name754");
  
      System.out.println("******** arrayListDuplicate **********");
      for (Student student : arrayListDuplicate3) {
          System.out.println(student.toString());
      }
      System.out.println("**************************\n");
  
      System.out.println("******** arrayListOrignal **********");
      for (Student student : arrayListOrignal) {
          System.out.println(student.toString());
      }
      System.out.println("**************************\n");
  </code></pre>
  
  <p><strong>OUTPUT FOR ALL THE METHODS:-</strong></p>
  
  <pre><code>******** arrayListOrignal **********
  Student:: serialNo: 1, serialNoDisplay:100, Name: name1
  Student:: serialNo: 2, serialNoDisplay:200, Name: name2
  **************************
  
  ******** arrayListDuplicate **********
  Student:: serialNo: 1, serialNoDisplay:100, Name: name1
  Student:: serialNo: 2, serialNoDisplay:754, Name: name754
  **************************
  
  ******** arrayListOrignal **********
  Student:: serialNo: 1, serialNoDisplay:100, Name: name1
  Student:: serialNo: 2, serialNoDisplay:754, Name: name754
  **************************
  </code></pre>
  
  <p>So I can see here, in all the tries(METHOD1, METHOD2, METHOD3) the Student object which is in arrayListOrignal get updated. </p>
  
  <p>But I am updating only Student object which is in arrayListDuplicate.</p>
  
  <p>So what is the best way to copy/clone ArrayList to other ArrayList and will not effect the fist ArrayList if we do any changes in the cloned ArrayList, even changes in side object of the cloned ArrayList.</p>
  
  <p>***** Question Updated *****</p>
  
  <p>Okkk...People are suggesting to go in this way <a href="https://stackoverflow.com/questions/715650/how-to-clone-arraylist-and-also-clone-its-contents">LINK</a>.</p>
  
  <p>But in my case this is not possible to modify the Student class.</p>
  
  <p>The above code is just an example.</p>