java - 在 Java 中更新 ArrayList 中的元素?

标签 java object arraylist

我有以下类(class):

public class Profile{
    String name, age, location;
}

假设我有以下代码:

ArrayList<Profile> profiles = somePopulatedArrayList;
Profile profile = profiles.get(1);
profile.name = "New name here";

我的问题是,当我有上述情况时,是 ArrayList 中对象的 .name 得到更新,还是我在这里创建一个全新的对象并且只更改 .name 对象,而存储在 ArrayList 中的 Profile 对象仍然具有旧名称?

我正在尝试编辑 ArrayList 中对象的属性,我想知道上述方法是否正确?

最佳答案

没有创建新对象。您已更新列表中的对象,即列表中的对象将使用“New name here”作为名称。

事实上,您可以使用调试器进行测试和查看。

关于java - 在 Java 中更新 ArrayList 中的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18447882/

相关文章:

java - 如何使用 SNMP4J 在 OID/托管对象上注册监听器以对代理收到的设置或获取请求进行操作?

java - 没有 includeantruntime 的 ant LinkageError

java - 使用 SwingUtilities.invokeLater() 更新文本字段

C++ 类 + 对象和用户交互

javascript - 对象分配键和值 - 如果值有效

java - 删除二维数组中的行的最快方法

java - 将文本文件中的某些整数读入数组列表

java - 为什么出于性能原因传递大字符串作为返回类型不好?

database - Visual Studio 2010 : Is there a way to deploy a single database object that retains the object in normal builds?

android - 检查数组列表是否在android中多次包含一个值