简体   繁体   English

在Java中更新相似的对象

[英]Updating similar objects in java

Let's say I have an Object called Index , this object has two attributes Long id; , String name; 假设我有一个名为Index的对象,该对象具有两个属性Long id; , String name; Long id; , String name; and I have two ArrayLists in my page, the problem is that when I edit the name of the index object in the first list it is being edited in the second list as well, here is my code to make the problem more understandable: 并且我的页面中有两个ArrayLists ,问题是当我在第一个列表中编辑索引对象的名称时,它也在第二个列表中进行编辑,这是我的代码,使问题更易于理解:

Index index1 = new Index();
index1.setName("1");
index1.setId(1);

List<Index> indexes = new ArrayList<Index>();
indexes.add(index1);

List<Index> newIndexes = new ArrayList<Index>();
newIndexes.add(index1);

Now if I update the name of the index in the indexes list it is being updated in the newIndexes list. 现在,如果我在indexes列表中更新indexes的名称,那么它将在newIndexes列表中更新。 note: the object index has equals method on the Id field. 注意:对象索引在Id字段上具有equals方法。

Thanks 谢谢

This happens because you add the same object into both lists or more precise... you add the reference to the same object into both lists. 发生这种情况的原因是,您将相同的对象添加到两个列表中或更精确的...将对同一对象的引用添加到两个列表中。

Thus when you update the object in one list it is updated in the second list, because it is the same object. 因此,当您在一个列表中更新对象时,由于它是同一对象,因此在第二个列表中更新了该对象。

Create a copy of the object before you add it to the second list, eg using a copy constructor . 在将对象添加到第二个列表之前,请创建该对象的副本 ,例如,使用副本构造函数

That is because index1 is just a reference to the object. 这是因为index1只是对对象的引用。 So, your are basically adding the same reference to both of the lists. 因此,您基本上将相同的引用添加到两个列表中。 You need to copy the object before adding to the second list. 您需要先复制对象,然后再添加到第二个列表。

When you add the object to both the lists, the reference of that object is copied to the lists. 当您将对象添加到两个列表中时,该对象的引用将被复制到列表中。 And that's why when you object the object from one list, it is reflected back in the other. 这就是为什么当您从一个列表中选择对象时,它会在另一个列表中反映出来的原因。 To avoid this, you need to create a copy of the object and add it to the other list, so that both do not refer to the same object. 为避免这种情况,您需要创建该对象的副本并将其添加到另一个列表中,以使两者都不会引用同一对象。

This is happening because when you use "Add" on array list (and in almost every data collection object) the collection is adding the "reference" of the object to its list, and not creating a new object. 发生这种情况的原因是,当您在数组列表(以及几乎每个数据收集对象)中使用“添加”时,集合会将对象的“引用”添加到其列表中,而不是创建新对象。 Thus, when both index1 objects at indexes and newIndexes are basically the same one. 因此,当indexes处的index1对象和newIndexes基本上相同时。 When changing it no matter where, it will be changed at the other as well. 无论何时何地进行更改,其他位置也将进行更改。

The reference index1 is the same for both lists, so changing the Index referenced will change it in both. 两个列表的引用index1相同,因此更改引用的Index将在两个列表中对其进行更改。

Cloning the List per se won't fix your issue, as it'll clone the List but not its elements. 克隆List本身无法解决您的问题,因为它将克隆List但不能克隆其元素。

You need to perform a deep-clone of the List and its elements (or initialize a new ArrayList , as you do, and clone each of the previous List 's elements) to solve your issue. 您需要执行的深克隆List 及其要素(或初始化一个新ArrayList ,因为你做的,每个克隆以前的List的元素),以解决您的问题。

See here for the how-to. 有关如何操作,请参见此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM