为什么 object 引用在它存储的 collection 中也没有改变?
Why object reference is not changed as well in the collection it's stored?
我正在玩一些代码,我期待不同的结果。例如,如果我有一些 Person
object,并且将它们添加到 collection(任何),后来修改它(一个或多个成员),不应该在 collection(s) 中也更新参考 referenced/added?
这是我 "playing" 使用的代码:
public static void main(String... args) {
Person robert = new Person("Robert", "Virding", 59);
Person mike = new Person("Mike", "Williams", 62);
Person joe = new Person("Joe", "Armstrong", 65);
List<Person> persons = new LinkedList<>();
persons.add(robert);
persons.add(mike);
persons.add(joe);
Collection<Person> immutable = Collections.unmodifiableCollection(persons);
joe = new Person("Joe", "Jonas", 35);
System.out.printf("[robert] %s%n", robert);
System.out.printf("[mike] %s%n", mike);
System.out.printf("[joe] %s%n", joe);
System.out.printf("[persons] %s%n", persons);
System.out.printf("[immutable] %s%n", immutable);
}
我的理解是object joe
应该是Person {givenName=Joe, familyName=Jonas, age=35}
而不是Person {givenName=Joe, familyName=Armstrong, age=65}
;但是 collection(s).
中的引用不会发生这种情况
有什么建议吗?
郑重声明,这是我得到的输出:
[robert] Person{givenName=Robert, familyName=Virding, age=59}
[mike] Person{givenName=Mike, familyName=Williams, age=62}
[joe] Person{givenName=Joe, familyName=Jonas, age=35}
[persons] [Person{givenName=Robert, familyName=Virding, age=59}, Person{givenName=Mike, familyName=Williams, age=62}, Person{givenName=Joe, familyName=Armstrong, age=65}]
[immutable] [Person{givenName=Robert, familyName=Virding, age=59}, Person{givenName=Mike, familyName=Williams, age=62}, Person{givenName=Joe, familyName=Armstrong, age=65}]
首先,当您创建对象joe
时,只有一个引用指向一个对象:
Person joe = new Person("Joe", "Armstrong", 65);
然后将其添加到列表中,创建对同一对象的新引用:
persons.add(joe);
现在有 2 个引用,一个是 joe
,另一个在集合 persons
中维护。现在在最后一步中,您更改参考:
joe = new Person("Joe", "Jonas", 35);
现在您已经创建了一个新对象,并为其创建了一个新引用。现在有 2 个引用和 2 个单独的对象:一个是引用第二个对象的 joe
,另一个在引用原始 joe
对象的集合内部维护。
您的列表引用了对象 ["Joe"、"Armstrong"、65],您的变量引用了第二个对象 ["Joe"、[=16] =], 35] 那是因为您创建了一个新对象并将其分配给 joe
。列表不保留对变量的引用,如 "joe",它包含对对象本身的引用。
我正在玩一些代码,我期待不同的结果。例如,如果我有一些 Person
object,并且将它们添加到 collection(任何),后来修改它(一个或多个成员),不应该在 collection(s) 中也更新参考 referenced/added?
这是我 "playing" 使用的代码:
public static void main(String... args) {
Person robert = new Person("Robert", "Virding", 59);
Person mike = new Person("Mike", "Williams", 62);
Person joe = new Person("Joe", "Armstrong", 65);
List<Person> persons = new LinkedList<>();
persons.add(robert);
persons.add(mike);
persons.add(joe);
Collection<Person> immutable = Collections.unmodifiableCollection(persons);
joe = new Person("Joe", "Jonas", 35);
System.out.printf("[robert] %s%n", robert);
System.out.printf("[mike] %s%n", mike);
System.out.printf("[joe] %s%n", joe);
System.out.printf("[persons] %s%n", persons);
System.out.printf("[immutable] %s%n", immutable);
}
我的理解是object joe
应该是Person {givenName=Joe, familyName=Jonas, age=35}
而不是Person {givenName=Joe, familyName=Armstrong, age=65}
;但是 collection(s).
有什么建议吗?
郑重声明,这是我得到的输出:
[robert] Person{givenName=Robert, familyName=Virding, age=59}
[mike] Person{givenName=Mike, familyName=Williams, age=62}
[joe] Person{givenName=Joe, familyName=Jonas, age=35}
[persons] [Person{givenName=Robert, familyName=Virding, age=59}, Person{givenName=Mike, familyName=Williams, age=62}, Person{givenName=Joe, familyName=Armstrong, age=65}]
[immutable] [Person{givenName=Robert, familyName=Virding, age=59}, Person{givenName=Mike, familyName=Williams, age=62}, Person{givenName=Joe, familyName=Armstrong, age=65}]
首先,当您创建对象joe
时,只有一个引用指向一个对象:
Person joe = new Person("Joe", "Armstrong", 65);
然后将其添加到列表中,创建对同一对象的新引用:
persons.add(joe);
现在有 2 个引用,一个是 joe
,另一个在集合 persons
中维护。现在在最后一步中,您更改参考:
joe = new Person("Joe", "Jonas", 35);
现在您已经创建了一个新对象,并为其创建了一个新引用。现在有 2 个引用和 2 个单独的对象:一个是引用第二个对象的 joe
,另一个在引用原始 joe
对象的集合内部维护。
您的列表引用了对象 ["Joe"、"Armstrong"、65],您的变量引用了第二个对象 ["Joe"、[=16] =], 35] 那是因为您创建了一个新对象并将其分配给 joe
。列表不保留对变量的引用,如 "joe",它包含对对象本身的引用。