简体   繁体   中英

Original ArrayList values Changed if i change something in Copied ArrayList. Even though i used List<Employee> empCopy=new ArrayList(origialList)

       for (int rowNum = 1; rowNum < sheet.getLastRowNum(); rowNum++) {
            Row row = sheet.getRow(rowNum);
            Employee instance = methodToGetInstance(parameters);
            originalList.add(instance);
        }
        List<Employee> copy = new ArrayList<>(originalList);
        List<Employee> totalCopy = new ArrayList<>(originalList);
        while (startDate.isBefore(ChronoLocalDate.from(SecurityUtils.now().plusMonths(3)))) {
            int index = 0;
            for (Employee instance : copy) {
                instance.setStartDateTime(startDateTime);       //here i'm updating the value in copyList
                instance.setEndDateTime(startDateTime.plusHours(24));
                totalCopy.add(instance);               
                index++;
            }
        }

Here im updating values in instance which is from Copied List it also affecting the OriginalList.

Please help me to resolve this issue...!!

It's pointers (references, in java parlance) all the way down.

A list object is essentially an address book. It's not a street.

When you write eg:

List<House> original = new ArrayList<House>();
// fill up 'original'
List<House> copy = original;
copy.remove(5);

Then both the original and the copy lost a page, as original and copy are both just references to the same address book. It's like original is a little postit with 'the address book is on the kitchen counter', and copy = original just makes a new post-it note and copies that text on the new note.

Taking the new note and following the instructions ( . is java-ese for: Follow the directions) gets you to the same address book.

But, all of java works like this, every where. If you make an actual full copy of the address book:

List<House> original = ...;
List<House> copy = new ArrayList<House>(original);

Then eg copy.remove(5) will no longer have an effect on original. But if you do copy.get(2).destroyHouse() , then the original is still affected. You didn't mess with the page in the address book, you messed with the house that you find when you drive over there.

if you want a complete copy, you'd have to:

  1. Take each page in your original address book and drive over there.
  2. Construct an identical copy of that house on some other street, at some address street number.
  3. Write this new street/streetnr in a new address book.

Now you have a true, complete copy.

How do you do that? Depends on the object. Address books (arraylists) can be copied with new ArrayList<>(refToExistingList) . How do you copy an Employee? Maybe new Employee(existingEmployeeRef) . Maybe existingRef.clone() . Maybe it's not possible. Maybe you'd have to manually write a utility method that painstakingly copies over every field (and this method would break if Employee gains another field, which it might, as that'd be API-compatible).

Change to

List<Employee> copy = new ArrayList<>();
copy = originalList;
List<Employee> totalCopy = new ArrayList<>();
while(startDate.isBefore(ChronoLocalDate.from(SecurityUtils.now().plusMonths(3)))) {
        int index = 0;
        for (Employee instance : copy) {
            instance.setStartDateTime(startDateTime);       //here i'm updating the value in copyList
            instance.setEndDateTime(startDateTime.plusHours(24));
            totalCopy.add(instance);               
            index++;
        }

Please think about the following code.

Point point = new Point(2, 1);
List<Point> a = Arrays.asList(point);
List<Point> b = Arrays.asList(point);

point.x = 1;

What will happens?

Both a and b have one element that references the point .
So when the point has been changed, it will look like the a and the b has been modified.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM