-1
       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 i m updating values in instance which is from Copied List it also affecting the OriginalList.

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

  • 1
    Yes, so you created a new list, pointing to _the same objects_. It doesn't matter which list you update those objects through. Think of it as having added another door to your library: it doesn't matter which door you walk through, any book you look at is still the same book. What you've done is made "shallow copies", not "deep copies". Search the web for those terms for all the details on their difference, it's an essential part to learn about the basics of java. – Mike 'Pomax' Kamermans Jun 08 '21 at 03:54
  • 1
    Does this answer your question? [How do you make a deep copy of an object?](https://stackoverflow.com/questions/64036/how-do-you-make-a-deep-copy-of-an-object) – Yoshikage Kira Jun 08 '21 at 03:55

3 Answers3

0

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 e.g:

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 e.g. 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).

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
0

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++;
        }
Dharman
  • 30,962
  • 25
  • 85
  • 135
LunaLissa
  • 45
  • 3
0

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.

Preston
  • 180
  • 9