为什么更改整个Array行会产生奇怪的行为?

mal*_*eki 2 java arrays

简单的说.为什么这会让我的代码在一段时间后出现问题.

//Color[][] colorArr = new Color[Width][Height]();

private void shiftRowsDown(int row) {
    for (int i = row; i > 0; i--)
    {
        colorArr[i] = colorArr[i - 1];//<--This in particular
    }
    for (int col = 0; col < colorArr[0].length; col++) 
    {
        colorArr[0][col] = null;
    }
}
Run Code Online (Sandbox Code Playgroud)

将其改为手动更改一个接一个很好.

private void shiftRowsDown(int row) {
    for (int i = row; i > 0; i--) {
        for(int col = 0;col < colorArr[i].length;col++)
        {
        colorArr[i][col] = colorArr[i - 1][col];//<--This in particular
        }
    }
    for (int col = 0; col < colorArr[0].length; col++) 
    {
        colorArr[0][col] = null;
    }
}
Run Code Online (Sandbox Code Playgroud)

Mat*_*hen 6

你有一个数组数组,所以你的第一个代码将外部数组的两个元素设置为相同的内部数组.

更简单的例子:

Color[][] colors = new Color[2][2];
colors[0] = new Color[]{Color.red, Color.blue}; // colors[0] holds a reference to an array object, located at, say, 0xcafebabe
colors[1] = new Color[]{Color.orange, Color.yellow}; // Say color[1] a reference to an array at 0xdeadbeef
Run Code Online (Sandbox Code Playgroud)

所以你可以想象颜色的记忆,如:

[0xcafebabe, 0xdeadbeef]
Run Code Online (Sandbox Code Playgroud)

如果你这样做:

colors[1] = colors[0];
Run Code Online (Sandbox Code Playgroud)

它是:

[0xcafebabe, 0xcafebabe]
Run Code Online (Sandbox Code Playgroud)

扩展的结构现在是:

{{Color.red, Color.blue}, {Color.red, Color.blue}}
Run Code Online (Sandbox Code Playgroud)

但是这两行都是在相同的内存位置引用同一个数组.如果你这样做:

colors[1][0] = Color.yellow;
Run Code Online (Sandbox Code Playgroud)

数组数组仍然是:

[0xcafebabe, 0xcafebabe]
Run Code Online (Sandbox Code Playgroud)

现在扩展的结构看起来像:

{{Color.yellow, Color.blue}, {Color.yellow, Color.blue}}
Run Code Online (Sandbox Code Playgroud)

这也称为浅拷贝.