1

I'm trying to create a new Object based on another one.

public Case(Case datCase){
        this.lstColors = (ArrayList<LineColor>) datCase.lstColors.clone();
        this.lstCases = (ArrayList<Case>) datCase.lstCases.clone();

        //this = (Case) datCase.clone();
    }

Of course i'm using the clone methods on every Object that i'm using (Not in the internal Objects like LineColor) but, it seems to not be cloning (The objects still change when the original changes)

I'm kind of newbie on this so please, help

PD: The complete project is here https://github.com/GunB/Mixing-Colours

GunBlade
  • 105
  • 2
  • 12

1 Answers1

0

All clone on the ArrayList does is that the ArrayList is a different than the first, but it still contains the same objects. You would have to iterate over all the objects and create new ones in the second ArrayList in order for them to be different.

Here is an example taken from How to clone ArrayList and also clone its contents?.

public static List<Dog> cloneList(List<Dog> dogList) {
    List<Dog> clonedList = new ArrayList<Dog>(dogList.size());
    for (Dog dog : dogList) {
        clonedList.add(new Dog(dog));
    }
    return clonedList;
}

This example actually uses a copy constructor instead of clone.

Community
  • 1
  • 1
rabz100
  • 751
  • 1
  • 5
  • 13