System.arrayCopy() 是复制对象还是对对象的引用? [英] System.arrayCopy() copies object or reference to object?

查看:37
本文介绍了System.arrayCopy() 是复制对象还是对对象的引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个期末班NameAndValue.我使用 System.arrayCopy() 复制了一个 NameAndValue 对象数组,当我更改复制数组中的 NameAndValue 对象时,它会反映在原始数组.

public final class NameAndValue {公共字符串名称;公共字符串值;公共名称和值(){}公共名称和值(字符串名称,字符串值){this.name = 名称;this.value = 值;}}

public class Main {公共静态无效主(字符串 [] args){NameAndValue[] nv = new NameAndValue[4];nv[0] = new NameAndValue(A", 1");nv[1] = new NameAndValue(B", 2");nv[2] = new NameAndValue(C", 3");nv[3] = new NameAndValue("D", "4");NameAndValue[] nv2 = new NameAndValue[4];System.arraycopy(nv, 0, nv2, 0, 2);nv2[2] = new NameAndValue("Y", "25");nv2[3] = new NameAndValue("Z", "26");for (int i = 0; i <2; i++) {NameAndValue[] nv3 = new NameAndValue[4];System.arraycopy(nv2, 0, nv3, 0, 4);nv3[2].value = String.valueOf(i);nv3[3].value = String.valueOf(i + 1);System.out.println(nv2[2].value);System.out.println(nv2[3].value);System.out.println(-----------------------");}}}

我得到的输出为:

<预><代码>01-----------------------12-----------------------

在所有情况下,我都应该得到 25 和 26 的输出,对吧??为什么要改?

解决方案

System.arrayCopy() 复制对象还是对象引用?

参考,它是一个副本.令人惊讶的是,文档没有明确说明,只是隐式说明,因为他们谈论复制数组元素,而不是递归复制他们参考的东西.

这和你有这个完全一样:

NameAndValue nv1 = new NameAndValue("A", "1");NameAndValue nv2 = nv1;nv2.value = "4";System.out.println(nv1.value);//4

每个数组元素就像上面的 nv1nv2 变量.正如 nv1nv2 引用(指向)相同的底层对象一样,数组条目也是如此,包括当这些条目从一个数组复制到另一个数组时.

I am having a final class NameAndValue. I copied an array of NameAndValue objects using System.arrayCopy() and when I changed a NameAndValue object in copied array, it gets reflected in the original array.

public final class NameAndValue {
    public String name;
    public String value;

    public NameAndValue() { }

    public NameAndValue(String name, String value) {
        this.name = name;
        this.value = value;
    }
}

public class Main {
    public static void main(String[] args) {
        NameAndValue[] nv = new NameAndValue[4];
        nv[0] = new NameAndValue("A", "1");
        nv[1] = new NameAndValue("B", "2");
        nv[2] = new NameAndValue("C", "3");
        nv[3] = new NameAndValue("D", "4");

        NameAndValue[] nv2 = new NameAndValue[4];
        System.arraycopy(nv, 0, nv2, 0, 2);
        nv2[2] = new NameAndValue("Y", "25");
        nv2[3] = new NameAndValue("Z", "26");

        for (int i = 0; i < 2; i++) {
            NameAndValue[] nv3 = new NameAndValue[4];
            System.arraycopy(nv2, 0, nv3, 0, 4);
            nv3[2].value = String.valueOf(i);
            nv3[3].value = String.valueOf(i + 1);

            System.out.println(nv2[2].value);
            System.out.println(nv2[3].value);
            System.out.println("-----------------------");
        }
    }
}

I am getting output as:

0
1
-----------------------
1
2
-----------------------

I should have got output as 25 and 26 in all the cases, right?? Why does it get changed??

解决方案

System.arrayCopy() copies object or reference to object?

Reference, it's a shallow copy. Surprisingly, the docs don't say that explicitly, just implicitly as they only talk about copying array elements, not recursively copying the things they reference.

It's exactly the same as if you had this:

NameAndValue nv1 = new NameAndValue("A", "1");
NameAndValue nv2 = nv1;
nv2.value = "4";
System.out.println(nv1.value); // 4

Each array element is like the nv1 and nv2 vars above. Just as nv1 and nv2 reference (point to) the same underlying object, so do the array entries, including when those entries are copied from one array to another.

这篇关于System.arrayCopy() 是复制对象还是对对象的引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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