一个对象数组的深层副本 [英] Deep copy of an object array

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

问题描述

我想用一个构造函数建立一个对​​象数组的深层副本。

I want to make a deep copy of an object array using a constructor.

public class PositionList {
    private Position[] data = new Position[0];

public PositionList(PositionList other, boolean deepCopy) {
        if (deepCopy){
            size=other.getSize();
            data=new Position[other.data.length];
            for (int i=0;i<data.length;i++){
            data[i]=other.data[i];
            }

不过,我上面有出于某种原因不能正常工作。我已经自动我运行测试,其失败的试验。所以,那里有一个错误在这里,林不知道它是什么。

However, what I have above for some reason is not working. I have automated tests that I run, and its failing those tests. So theres an error an here that Im not sure what it is.

推荐答案

什么您已经实现了一个复制。要实现复制,你必须
变化

What you have implemented is a shallow copy. To implement a deep copy, you must change

data[i] = other.data[i];

有些东西 other.data [I] 复制分配给数据[I] 。你是如何做到这依赖于位置类。可能的替代方案是:

to some thing that assigns a copy of other.data[i] to data[i]. How you do this depends on the Position class. Possible alternatives are:


  • 拷贝构造函数:

  • a copy constructor:

数据[I] =新的位置(other.data [I]);

工厂方法:

数据[I] = createPosition(other.data [I]);

克隆

数据[I] =(位置)other.data [I] .clone();

注:


  1. 上述假设拷贝构造函数,工厂方法和克隆方法分别实现了正确的一种复制,取决于位置类;请看下文。

  2. 克隆办法只会工作,如果位置明确支持它,这通常被认为是一个劣解。此外,你需要注意,克隆的本地实现(即 Object.clone()法)不浅表副本。

  1. The above assume that the copy constructor, factory method and clone method respectively implement the "right" kind of copying, depending on the Position class; see below.
  2. The clone approach will only work if Position explicitly supports it, and this is generally regarded as an inferior solution. Besides, you need to be aware that the native implementation of clone (i.e. the Object.clone() method) does a shallow copy.

在实际上执行用Java深度复制的一般问题是复杂的。在位置的情况下类,人们会假定属性是所有基本类型(例如整数或双打),因此深深浅浅与复制是没有实际意义。但如果有参考属性,那么你必须依靠拷贝构造函数/工厂方法/ clone方法做,你需要的那种复制。在每一种情况下,需要进行编程。而在一般情况下(在那里你必须处理周期)是困难的,要求每个类来实现特殊的方法。

In fact the general problem of implementing deep copying in Java is complicated. In the case of the Position class, one would assume that the attributes are all primitive types (e.g. ints or doubles), and therefore a deep versus shallow copying is moot. But if there are reference attributes, then you have to rely on the copy constructor / factory method / clone method to do the kind of copying that you require. In each case it needs to be programmed in. And in the general case (where you have to deal with cycles) it is difficult and requires each class to implement special methods.

有一个其他的潜力的方式来复制对象的数组。如果数组中的对象的序列化的,那么你可以通过将它们复制的ObjectOutputStream 的ObjectInputStream 序列化和反序列化,然后数组。但是:

There is one other potential way to copy an array of objects. If the objects in the array are serializable, then you can copy them by using ObjectOutputStream and ObjectInputStream serialize and then deserialize the array. However:


  • 这是昂贵的,

  • 它只如果对象(及物动词)序列化的作品,和

  • 瞬态字段将不会被复制。
  • 的值
  • this is expensive,
  • it only works if the objects are (transitively) serializable, and
  • the values of any transient fields won't be copied.

不建议通过序列化复制。这将是更好地支持克隆或其他方法。

Copying by serialization is not recommended. It would be better to support cloning or some other method.

所有的一切,深度复制在Java中最好避免。

All in all, deep copying is best avoided in Java.

最后,回答您有关位置类的拷贝构造函数的作品的问题,我希望它是这样的:

Finally, to answer your question about the Position classes copy constructor works, I expect it is something like this:

public class Position {
    private int x;
    private int y;
    ...
    public Position(Position other) {
        this.x = other.x;
        this.y = other.y;
    }
    ...
}

由于@Turtle说,有没有涉及魔法。您实现从现有的实例初始化复制其状态的构造(用手)。

As @Turtle says, there's no magic involved. You implement a constructor (by hand) that initializes its state by copying from an existing instance.

这篇关于一个对象数组的深层副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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