java - 新对象引用旧对象
问题描述
我有一个像这样的简单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 是否有原因?有解决方案吗?
提前致谢
解决方案
推荐阅读
- .net - 使用 C# 将数据从 Excel 插入数据库
- javascript - 在php中确认后删除
- jquery - Jquery 显示不仅仅适用于具有特定类的元素
- mysql - 选择语句失败 SQL Server
- mongodb - 在 find() 函数中
- spring - Webflux multipart/form-data,启用 csrf,有和没有文件上传获取 Invalid CSRF Token
- .net - Wix Bootstrapper 托管应用程序升级问题
- typescript - 创建实例时,打字稿类装饰器可以是可选的吗?
- webpack - Webpack 编译显示 bootstrap 3 sass 的错误
- sql-server - Tsql 用其他表更新表