为什么更改整个阵列行创建古怪的行为? [英] Why does changing an entire Array row create odd behavior?

查看:68
本文介绍了为什么更改整个阵列行创建古怪的行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单地说。为什么会这样做一段时间后,我的code故障。

  //颜色[] [] = colorArr新的色彩[宽度] [身高();私人无效shiftRowsDown(INT行){
    的for(int i =行; I> 0;我 - )
    {
        colorArr [I] = colorArr [我 - 1]; //< - 这尤其
    }
    对于(INT COL = 0;&山坳下,colorArr [0]。长度;西++)
    {
        colorArr [0] [山口] =无效;
    }
}

而将其更改为手动更改一个个都很好。

 私人无效shiftRowsDown(INT行){
    的for(int i =行; I> 0;我 - ){
        对于(INT COL = 0;&山坳下,colorArr [I]。长度;山坳++)
        {
        colorArr [I] [COL] = colorArr [我 - 1] [COL]; //< - 这尤其
        }
    }
    对于(INT COL = 0;&山坳下,colorArr [0]。长度;西++)
    {
        colorArr [0] [山口] =无效;
    }
}


解决方案

您有一个数组的数组,所以你的第一个$ C $账套外阵列中的两个要素相同的内部数组。

简单的例子:

 颜色[] [] =颜色新的色彩[2] [2];
颜色[0] =新的色彩[] {Color.red,Color.blue}; //颜色[0]持​​有引用数组对象,位于比如说,0xcafebabe
颜色[1] =新的色彩[] {Color.orange,Color.yellow}; //说颜色[1]在0xdeadbeef数组的引用

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

  [0xcafebabe,0xdeadbeef]

如果你然后做:

 颜色[1] =颜色[0];

是:

  [0xcafebabe,0xcafebabe]

膨胀的结构现在是:

  {{Color.red,Color.blue},{Color.red,Color.blue}}

但两者行是对的相同的阵列,在的相同的内存位置引用。如果你然后做:

 颜色[1] [0] = Color.yellow;

阵列的阵列仍然

  [0xcafebabe,0xcafebabe]

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

  {{Color.yellow,Color.blue},{Color.yellow,Color.blue}}

这也被称为浅拷贝

Simply put. Why did this make my code malfunction after awhile.

//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;
    }
}

while changing it to manually change one by one was fine.

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;
    }
}

解决方案

You have an array of arrays, so your first code sets two elements of the outer array to the same inner array.

Simpler example:

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

So you can visualize colors' memory like:

[0xcafebabe, 0xdeadbeef]

If you then do:

colors[1] = colors[0];

it is:

[0xcafebabe, 0xcafebabe]

The expanded structure is now:

{{Color.red, Color.blue}, {Color.red, Color.blue}}

But both rows are references to the same array, at the same memory position. If you then do:

colors[1][0] = Color.yellow;

the array of arrays is still:

[0xcafebabe, 0xcafebabe]

and the expanded structure now looks like:

{{Color.yellow, Color.blue}, {Color.yellow, Color.blue}}

This is also called a shallow copy.

这篇关于为什么更改整个阵列行创建古怪的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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