0

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.

Student POJO class:-

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;
}
}

InSide Main Method:-

    ArrayList<Student> arrayListOrignal = new ArrayList<Student>();

    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<Student> arrayListDuplicate = new ArrayList<Student>(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<Student> arrayListDuplicate2 = (ArrayList<Student>) 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<Student> arrayListDuplicate3 = new ArrayList<Student>();
    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");

OUTPUT FOR ALL THE METHODS:-

******** 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
**************************

So I can see here, in all the tries(METHOD1, METHOD2, METHOD3) the Student object which is in arrayListOrignal get updated.

But I am updating only Student object which is in arrayListDuplicate.

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.

***** Question Updated *****

Okkk...People are suggesting to go in this way LINK.

But in my case this is not possible to modify the Student class.

The above code is just an example.

Vinit ...
  • 1,409
  • 10
  • 37
  • 66
  • Thanks buddy...but the problem is I can't modify the Student class. Above code is just an example. Basically I am getting one ArrayList and want to clone. After that I am modifying the cloned ArrayList. – Vinit ... Sep 16 '19 at 20:19
  • Try this ArrayList arrayListDuplicate3 = new ArrayList(arrayListOrignal.size()); // arrayListDuplicate3.addAll(arrayListOrignal); // arrayListDuplicate3.get(1).setRollNo(754); // arrayListDuplicate3.get(1).setName("name754"); for(Student s : arrayListOrignal) { arrayListDuplicate3.add(new Student(s.getSerialNo(), 756, "name756")); } – anirban Sep 18 '19 at 08:33

0 Answers0