首页 > 解决方案 > 新对象引用旧对象

问题描述

我有一个像这样的简单Java程序

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

和我的主要课程中的一个方法

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;
}

因此 myObjectList 不断变化,直到 anotherObjectList 具有理想的形式。逻辑上这似乎是正确的,但即使 isMyObjectListIdeal() 为假,当我在 makeSomeChangesInEachAnotherObjectList 中更改 myObjectList 的形成时,理想主义者似乎也改变了它的值

理想主义者和 myObjectList 都指向同一个 anotherObjectList 是否有原因?有解决方案吗?

提前致谢

标签: javareference

解决方案


推荐阅读