恒定值的变化,无论是否恒定;此外,对一行2D阵列的更改也会传播给其他人

时间:2015-12-06 16:08:00

标签: javascript arrays constants

当我偶然发现一个烦人的问题时,我正试图做AdventOfCode.com的第六天,我不知道原因。我

var input = ["turn on 7,6 through 9,6","turn on 1,3 through 6,9"];
var grid = 0;

function Create2Darray(dimension) {
    var arr = [0];
    var arr2 = [0];

    for (i=0; i<dimension; i++) {
        arr2[i] = 0;
    }
    for (k=0; k<dimension; k++) {
        arr[k] = arr2;
    }
    return arr;
}

grid = Create2Darray(10);
const p = grid; // THIS IS WHAT IT IS ALL ABOUT

temp = grid[4];
    temp[5] = 3;
grid[4] = temp;

p; // outputs [[0, 0, 0, 0, 0, 3, 0, 0, 0, 0], [0, 0, 0, 0, 0, 3, 0, 0, 0, 0], [0, 0, 0, 0, 0, 3, 0, 0, 0, 0], [0, 0, 0, 0, 0, 3, 0, 0, 0, 0], [0, 0, 0, 0, 0, 3, 0, 0, 0, 0], [0, 0, 0, 0, 0, 3, 0, 0, 0, 0], [0, 0, 0, 0, 0, 3, 0, 0, 0, 0], [0, 0, 0, 0, 0, 3, 0, 0, 0, 0], [0, 0, 0, 0, 0, 3, 0, 0, 0, 0], [0, 0, 0, 0, 0, 3, 0, 0, 0, 0]] to console.
// Although we said: const p = grid;
// And at that time, grid was equal to [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 3, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]

其次 这样:

temp = grid[4];
temp[5] = 3;
grid[4] = temp;

我的期望是:

[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 3, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]

但我得到了:

[[0, 0, 0, 0, 0, 3, 0, 0, 0, 0], [0, 0, 0, 0, 0, 3, 0, 0, 0, 0], [0, 0, 0, 0, 0, 3, 0, 0, 0, 0], [0, 0, 0, 0, 0, 3, 0, 0, 0, 0], [0, 0, 0, 0, 0, 3, 0, 0, 0, 0], [0, 0, 0, 0, 0, 3, 0, 0, 0, 0], [0, 0, 0, 0, 0, 3, 0, 0, 0, 0], [0, 0, 0, 0, 0, 3, 0, 0, 0, 0], [0, 0, 0, 0, 0, 3, 0, 0, 0, 0], [0, 0, 0, 0, 0, 3, 0, 0, 0, 0]]

我做错了什么?

所以核心问题:

  • 为什么CONSTANT p会改变?
  • 为什么主阵列中的所有“子阵列”都发生了变化,而我只选择了第4个“子阵列”中的第5个值?

2 个答案:

答案 0 :(得分:3)

关于参考/复制

Javascript通过“引用”工作,而不是像C ++那样“通过复制”工作。这意味着:

 for (int i = 0; i < level; i++)
            {
                for (int j = 0; j < level; j++)
                    Console.Write(string.Format("{0}   ", matrix[i][j].ToString("0.00")));
                Console.Write(Environment.NewLine + Environment.NewLine);
            }

var a = [1, 2, 3]; var b = [a, a]; 是一个数组,包含对同一数组b的两个引用,而不是a的两个副本。例如

之后
a

b[0][0] = 99; 也是b[1][0],因为99b[0]是对同一个对象的引用。

如果要构建矩阵,则需要单独构建每一行...例如:

b[1]

关于const

声明对数组的var grid = []; for (var i=0; i<100; i++) { grid.push(new Array(100)); } // Now grid is a matrix of 100x100 undefined elements 引用不会阻止对数组内容进行修改,只能阻止重新分配const以引用其他内容。

答案 1 :(得分:2)

创建数组副本的快捷方法是使用slice(0),例如代替

for (k=0; k<dimension; k++) {
    arr[k] = arr2;
}

你可以

for (k=0; k<dimension; k++) {
    arr[k] = arr2.slice(0);
}

然后代码按预期工作:2D数组的每一行都具有相同的内容,但行可以独立修改。另见:What's the point of .slice(0) here?