简体   繁体   English

如何创建ArrayList的副本 <ArrayList<Arc> &gt;到另一个ArrayList <ArrayList<Arc> &gt;

[英]How to create a copy of ArrayList<ArrayList<Arc>> to another ArrayList<ArrayList<Arc>>

I have a Class 我有一堂课

public class Arc{
...
}

and 2 ArrayList 和2 ArrayList

ArrayList<ArrayList<Arc>> rotalar1 = new ArrayList<ArrayList<Arc>>();
ArrayList<ArrayList<Arc>> rotalar2 = new ArrayList<ArrayList<Arc>>();

i try to copy rotalar1 to in rotalar2: 我尝试将rotalar1复制到rotalar2中:

rotalar2.addAll(rotalar1);

but i have a problem. 但我有一个问题。 if i make any change in rotalar2 , it has an impact on rotalar1 too. 如果我对rotalar2进行了任何更改,它也会对rotalar1产生影响。 I dont want to make a change in rotalar1 : 我不想在rotalar1进行更改:

These rows make problem 这些行造成问题

rotalar2.get(random1).remove(random3);
rotalar2.get(random2).remove(random4);

Thanks for your time 谢谢你的时间

Method addAll does shallow copy which means it copies all the reference of object (not actual Object) from rotalar1 to rotalar2 . 方法addAll浅表复制,这意味着它将对象(不是实际Object)的所有引用从rotalar1rotalar2

  • One way, you need to iterate over each object( Arc ) and clone it and add it to new list. 一种方法是,您需要遍历每个对象( Arc )并将其克隆并将其添加到新列表中。
  • One other way is deep copy using serialization. 另一种方法是使用序列化进行深度复制。 Example Code 范例程式码

Iterate over rotalar1 an copy each list of this list to rotalar2. 遍历rotalar1,将此列表的每个列表复制到rotalar2。 You can make the copy in different ways. 您可以用不同的方式制作副本。 In the first example i use the constructor and create a new list with the same elements. 在第一个示例中,我使用构造函数并使用相同的元素创建一个新列表。 Be careful, if you make changes to the Arc objects, this changes will still take effect in both lists. 请注意,如果对Arc对象进行更改,则此更改在两个列表中仍将生效。 If you dont want this, you have to copy your Arc objects too. 如果您不希望这样做,则也必须复制Arc对象。

    ArrayList<ArrayList<Arc>> rotalar1 = new ArrayList<ArrayList<Arc>>();
    ArrayList<ArrayList<Arc>> rotalar2 = new ArrayList<ArrayList<Arc>>();

    for(ArrayList<Arc> list : rotalar1)
    {
        rotalar2.add(new ArrayList<Arc>(list));
    }

This is another way, using Collections.copy(dest,src) 这是使用Collections.copy(dest,src)另一种方法

    for(ArrayList<Arc> list : rotalar1)
    {
        ArrayList<Arc> copy = new ArrayList<>();
        Collections.copy(copy, list);  //or copy.addAll(list);
        rotalar2.add(copy);
    }

This problem is soved now: 现在解决此问题:

rotalar2.get(random1).remove(random3);
rotalar2.get(random2).remove(random4);

but this will still take effect on both lists: 但这仍将在两个列表上均生效:

rotalar2.get(rndList).get(rndArc).set(xvy)

If you want to fix this problem too, you can do something like this: 如果您也想解决此问题,则可以执行以下操作:

    ArrayList<ArrayList<Arc>> rotalar1 = new ArrayList<ArrayList<Arc>>();
    ArrayList<ArrayList<Arc>> rotalar2 = new ArrayList<ArrayList<Arc>>();

    for(ArrayList<Arc> list : rotalar1)
    {
        ArrayList<Arc> tmpList = new ArrayList<>();
        for(Arc arcObj : list)
        {
            tmpList.add(copyOfyourArc); //TODO how do you want to creat a copy of Arc obj?
        }
        rotalar2.add(tmpList);
    }

You are not deep copying your list. 您没有深度复制列表。

The problem is, that your lists contain references to objects. 问题是您的列表包含对对象的引用。 If you copy that (list of) refereces to your second list, both references (the original and the copied one) are pointing to the very same object. 如果将该引用(列表)复制到第二个列表,则两个引用(原始引用和复制的引用)都指向同一对象。 Every change applied to the object through the referenc in the second list, will be visible in the first list as well. 通过第二个列表中的引用应用于对象的每个更改,也将在第一个列表中可见。

To deep copy the collection, you could eg iterate over it and manually copy each index. 要深层复制集合,您可以例如对其进行迭代并手动复制每个索引。

You should implement your own method to do a deep copy within the Arc class. 您应该实现自己的方法以在Arc类中进行深层复制。 The method should create a new instance of the object and set all fields(attributes) to the value that the current object has. 该方法应创建该对象的新实例,并将所有字段(属性)设置为当前对象具有的值。 This method should return a new Object of type Arc that has all the same values as the object trying to be copied. 此方法应返回一个新的Arc类型的Object,该对象的所有值都与尝试复制的对象相同。 You should then iterate over your arraylist(s) and use your copy method on each object and add the new object to a new arraylist which will be a copy of your original arraylist. 然后,您应该遍历您的arraylist,并在每个对象上使用copy方法,然后将新对象添加到新的arraylist中,这将是原始arraylist的副本。 Example: 例:

//I don't know the attributes of your Arc class this is just an example

public Arc deepCopy()
{
    Arc copyOfArc = new Arc(this.field, this.field2, this.field3, this.field4, etc...);
    //if you don't set everything in the constructor then you should use your setters here.
    copyOfArc.setField5(this.Field5);
    etc...
    return copyOfArc;
}

Use this method to iterate over your arraylist and copy each arc object to a new arraylist. 使用此方法可以遍历数组列表并将每个弧对象复制到新的数组列表。

Here is an example of using the deepCopy() method to copy your objects to new arraylist: 这是使用deepCopy()方法将对象复制到新的arraylist的示例:

ArrayList<ArrayList<Arc>> rotalar2 = new ArrayList<ArrayList<Arc>>();
int counter = 0;

for(ArrayList<Arc> temp : rotalar1)
{
   rotalar2.add(new ArrayList<Arc>());
   for(Arc temp1 : temp)
   {
      rotalar2.get(counter).add(temp1.deepCopy());
   }
   counter++;
}

This will copy arraylist rotalar1 to arraylist rotalar2 with a deepCopy so each arraylist will have its own objects and won't effect each other anymore. 这将使用deepCopy将arraylist rotalar1复制到arraylist rotalar2,因此每个arraylist将具有其自己的对象,并且不再相互影响。

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

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