0

I have a simple Java program like this

public Class MyObject {
    List<anotherObject> anotherObjectList;
    int average;
}

and a method in my main class

List<MyObject> getIdealList(List<MyObject> myObjectList){
    List<MyObject> idealList = new Arraylist<>(myObjectList);
    for(int i = 0; i< myObjectList.size() ; i++)
    {
        myObjectList = makeSomeChangesInEachAnotherObjectList(myObjectList)
        if(isMyObjectListIdeal(myObjectList))
        {
             idealList = new Arraylist<>(myObjectList);
        }    
    }
    return idealList;
}

So the myObjectList is constantly changed until the anotherObjectList has the ideal formation. Logically this seems correct, but even when isMyObjectListIdeal() is false, when I change the formation of myObjectList in makeSomeChangesInEachAnotherObjectList, the idealist also seems to change it's value

Is there a reason why both idealist and myObjectList points to the same anotherObjectList? Is there a solution for this?

Thanks in Advance

Visahan
  • 1,130
  • 2
  • 14
  • 35
  • What exact code for functions makeSomeChangesInEachAnotherObjectList(myObjectList) and isMyObjectListIdeal(myObjectList), You might be changing the values of object in "myObjectList" and return the new list with same object. in this case return and passed list both have update data. like wise isMyObjectListIdeal(myObjectList) function would be doing. – Pandey Amit Oct 05 '18 at 06:38
  • @Amin " so any change in each of list, reflect both of them because object are same only in different container." It is not true , The changes does not reflect – Tarun Oct 05 '18 at 06:47
  • 1
    @Tarun I mean "idealList" is not deep copy of "myObjectList" so if we send "myObjectList" to another method and change its values (change list values directly not replace them with new object), this values changed in "idealList" (both reference to same place) as far as I know. Is it not correct? Did I make a mistake? – Amin Oct 05 '18 at 06:51
  • @Amin Yes you are not correct , Suggest you tto produce scenario and try it yourself to get it clear – Tarun Oct 05 '18 at 06:54
  • @Tarun creating a new list wit: _new Arraylist<>(myObjectList);_ does NOT make a deep copy that's what Amin put in his words. And he is right about this – star67 Oct 05 '18 at 07:03
  • @Tarun. I doesn't work until now with chat room but can you come in this room: "Ask about problem" I think that I didn't understand what you said. – Amin Oct 05 '18 at 07:03
  • @star67 I am not saying about deep copy or something else, only this _"so any change in each of list, reflect both of them because object are same only in different container."_ is not true – Tarun Oct 05 '18 at 07:10
  • 1
    @Tarun he put in his words _"any change in each of list, reflect both of them"_ the following meaning: IF you **change state of object1** which is in both lists (and it is on both lists because the 2nd list is a shallow copy of the 1st list) - it would change also list2 because object1 is in both – star67 Oct 05 '18 at 07:35
  • What @Amin said worked, I had to create a deep copy so that both objects don't have the same reference. It's manual loops & Clones which did the trick. Wonder why we cannot create deep copies in a more simpler way. – Visahan Oct 06 '18 at 08:02
  • @star67 Bottom line is:- new Arraylist<>(myObjectList); is shallow copy , and changes done in **objects which are stored in either list** will reflect in both list . BUT changes done **in list itself** (changes like delete, insert etc objects in list) will Not reflect in other list. – Tarun Oct 11 '18 at 12:49
  • @Tarun for sure, this does not contradict with my message above =) – star67 Oct 11 '18 at 12:57
  • @star Yes, i commented for someone who will read in future, and we were both right therefore i explained...you were saying 1 point and i was saying 2 point – Tarun Oct 11 '18 at 12:59

0 Answers0