Java 数组的克隆方法 [英] Clone method for Java arrays

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

问题描述

Java 中的 clone() 方法在数组上使用时究竟返回什么?它是否返回一个从原始数组复制数据的新数组?

What exactly does the clone() method in Java return when used on an array? Does it return a new array with data copied from the original?

例如:

int[] a = {1,2,3};
int[] b = a.clone();

推荐答案

当对数组调用 clone 方法时,它会返回对包含(或引用)相同数组的新数组的引用元素作为源数组.

When the clone method is invoked upon an array, it returns a reference to a new array which contains (or references) the same elements as the source array.

因此,在您的示例中,int[] a 是在堆上创建的单独对象实例,而 int[] b 是在堆上创建的单独对象实例.(记住所有数组都是对象).

So in your example, int[] a is a separate object instance created on the heap and int[] b is a separate object instance created on the heap. (Remember all arrays are objects).

    int[] a = {1,2,3};
    int[] b = a.clone();

    System.out.println(a == b ? "Same Instance":"Different Instance");
    //Outputs different instance

如果要修改int[] b,则更改将不会反映在int[] a 上,因为两者是单独的对象实例.

If were to modify int[] b the changes would not be reflected on int[] a since the two are separate object instances.

    b[0] = 5;
    System.out.println(a[0]);
    System.out.println(b[0]);
    //Outputs: 1
    //         5

当源数组包含对象时,这会变得稍微复杂一些.clone 方法将返回对新数组的引用,该数组引用与源数组相同的对象.

This becomes slightly more complicated when the source array contains objects. The clone method will return a reference to a new array, which references the same objects as the source array.

所以如果我们有 Dog...

So if we have the class Dog...

    class Dog{

        private String name;

        public Dog(String name) {
            super();
            this.name = name;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

    }

然后我创建并填充一个 Dog...

and I create and populate an array of type Dog...

    Dog[] myDogs = new Dog[4];

    myDogs[0] = new Dog("Wolf");
    myDogs[1] = new Dog("Pepper");
    myDogs[2] = new Dog("Bullet");
    myDogs[3] = new Dog("Sadie");

然后克隆狗...

    Dog[] myDogsClone = myDogs.clone();

数组引用相同的元素...

the arrays refer to the same elements...

    System.out.println(myDogs[0] == myDogsClone[0] ? "Same":"Different");
    System.out.println(myDogs[1] == myDogsClone[1] ? "Same":"Different");
    System.out.println(myDogs[2] == myDogsClone[2] ? "Same":"Different");
    System.out.println(myDogs[3] == myDogsClone[3] ? "Same":"Different");
    //Outputs Same (4 Times)

这意味着如果我们修改一个通过克隆数组访问的对象,那么当我们访问源数组中的同一个对象时,更改将反映,因为它们指向相同的引用.

This means if we modify an object accessed through the cloned array, the changes will be reflected when we access the same object in the source array, since they point to the same reference.

    myDogsClone[0].setName("Ruff"); 
    System.out.println(myDogs[0].getName());
    //Outputs Ruff

但是,对数组本身的更改只会影响该数组.

However, changes to the array itself will only affect that array.

    myDogsClone[1] = new Dog("Spot");
    System.out.println(myDogsClone[1].getName());
    System.out.println(myDogs[1].getName());
    //Outputs Spot
    //        Pepper

如果您大致了解对象引用的工作原理,就很容易理解克隆和修改对对象数组的影响.为了进一步了解引用和原语,我建议阅读这篇优秀的文章.

If you generally understand how object references work, it is easy to understand how arrays of objects are impacted by cloning and modifications. To gain further insight into references and primitives I would suggest reading this excellent article.

源代码要点

这篇关于Java 数组的克隆方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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