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