数组列表的复制构造函数 [英] Copy constructor of Array List

查看:37
本文介绍了数组列表的复制构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚阅读了人们认为的 ArrayList 的深拷贝

I just read about deep copy of ArrayList, that people think

new ArrayList<>(originalList);

将创建一个浅拷贝.而且我写了一个小demo

will create a shallow copy. And I wrote a small demo

    ArrayList<String> originalNameList = new ArrayList<>(Arrays.asList("Anna", "Betty", "Chris"));
    List<String> copyNameList1=originalNameList;
    List<String> copyNameList2= new ArrayList<>(originalNameList);

    originalNameList.add("Duke");
    copyNameList1.add("Ellen");
    copyNameList1.set(1,"Bill");
    copyNameList2.set(0,"Andy");

    System.out.println("originalNameList = " + originalNameList);
    System.out.println("copyNameList1    = " + copyNameList1);
    System.out.println("copyNameList2    = " + copyNameList2);

结果:

originNameList = [Anna, Bill, Chris, Duke, Ellen]
copyNameList1  = [Anna, Bill, Chris, Duke, Ellen]
copyNameList2  = [Andy, Betty, Chris]

感觉ArrayList的拷贝构造函数没那么浅.那为什么人们会这样说?是否存在某些级别的深拷贝?非常感谢!

I feel that copy constructor of ArrayList is not that shallow. That why people say that so? Are there some levels of deep copy? Thank a lot!

推荐答案

您对浅"和深"复制的定义似乎是错误的.您似乎认为浅拷贝意味着复制对对象的引用",如下所示:

Your definitions for "shallow" and "deep" copy seem to be wrong. It seems like you think a shallow copy means "copying the reference to the object", like this:

List<String> copyNameList1=originalNameList;

而深"复制会创建一个新的数组列表:

Whereas a "deep" copy creates a new array list:

List<String> copyNameList2= new ArrayList<>(originalNameList);

请注意,以上是一种误解.

Note that the above is a misconception.

浅拷贝实际上意味着创建一个具有相同元素的新数组列表,而深拷贝意味着创建一个具有原始数组列表中元素副本的新数组列表.

A shallow copy actually means creating a new array list with the same elements, while a deep copy means creating a new array list that has copies of the elements in the original array list.

这里最大的区别是数组列表中的元素是否被复制".

The big difference here is "whether the elements in the array list are copied".

假设您有一个数组列表对象 a1.它包含对 3 个三个对象的引用 obj1obj2obj3.

Imagine you have an array list object a1. It contains references to 3 three objects obj1, obj2, obj3.

a1 的浅拷贝将创建一个新的数组列表对象 a2,其中包含对相同 obj1obj2<的引用/code>, obj3 对象.

A shallow copy of a1 will create a new array list object a2 that contains references to the same obj1, obj2, obj3 objects.

a1 的深层副本将创建一个新的数组列表对象 a2,其中包含对 obj4obj5 的引用>、obj6,其中obj4obj5obj6obj1 的副本,obj2obj3 分别.

A deep copy of a1 will create a new array list object a2 that contains references to obj4, obj5, obj6, where obj4, obj5 and obj6 are copies of obj1, obj2, and obj3 respectively.

这里有两种方法显示了浅拷贝和深拷贝中发生的情况:

Here are two methods that shows what happens in shallow copy and deep copy:

static ArrayList<String> shallowCopy(ArrayList<String> original) {
    ArrayList<String> copy = new ArrayList<>();
    for (String s : original) {
        copy.add(s);
    }
    return copy;
}

static ArrayList<String> deepCopy(ArrayList<String> original) {
    ArrayList<String> copy = new ArrayList<>();
    for (String s : original) {
        String copyOfString = new String(s); // note that each string is copied as well
        copy.add(copyOfString);
    }
    return copy;
}

一般来说,深拷贝也会复制目标对象所引用的对象.

In general, a deep copy copies the objects that the target object refers to as well.

这篇关于数组列表的复制构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆