【问题标题】:Why is my current object modified when I use an instance method to return another object? - Java为什么当我使用实例方法返回另一个对象时,我的当前对象被修改了? - 爪哇
【发布时间】:2013-03-26 15:08:46
【问题描述】:

我有这门课,但我不明白到底发生了什么:

public class Table {
    private int[][] array;
    private int N;

    // constructor
    public Table(int[][] array) {
        N = array.length;
        this.array = Arrays.copyOf(array, N);
    }

    // this method returns another Table object
    public Table tweak() {
        int[][] tweak = Arrays.copyOf(array, N);
        // here I change the array
        return new Table(tweak);
    }
}

问题是当我调用tweak方法时,用于调用该方法的对象也发生了变化:

public class TestCls {
    public static void main(String[] args) {
        int[][] array = {{1, 2}, 
                         {3, 4}};
        Table a = new Table(array);
        System.out.println(a.toString());
        /* this will print
        *  1  2 
        *  3  4
        */

        Table b = a.tweak();
        System.out.println(b.toString());
        /* this will print
        *  2  1 
        *  3  4
        */

        System.out.println(a.toString());
        /* this will also print
        *  2  1 
        *  3  4
        */
    }
}

请帮助我了解为什么会发生这种情况以及我应该如何更改调整方法以使原始对象不被更改。

谢谢

【问题讨论】:

    标签: java object instance


    【解决方案1】:

    Arrays.copyOf 正在创建一个新的 int[][],但这需要 副本 - 它相当于:

    int[][] tweak = new int[array.length][];
    for (int i = 0; i < tweak.length; i++) {
        tweak[i] = array[i];
    }
    

    因此,虽然您有一个新的“顶级”数组,但每个“子数组”都是共享的。你真的想制作一个数组的 deep 副本:

    int[][] tweak = new int[array.length][];
    for (int i = 0; i < tweak.length; i++) {
        tweak[i] = array[i].clone(); // Simpler way of performing a shallow copy
    }
    

    这是对每个“子数组”执行浅拷贝,但这很好,因为元素类型只是 int(没有办法让它“更深”)。

    请注意,您只需要在tweak 方法中 在构造函数中进行此。复制两份是没有意义的。

    【讨论】:

      【解决方案2】:

      问题在于Table 构造函数生成了数组的shallow copy

      How do I do a deep copy of a 2d array in Java?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-08-03
        • 2011-07-12
        • 2020-04-02
        • 2013-05-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多