锯齿状数组混乱

Jagged array confusion

我想制作一个锯齿状数组 矩阵 来存储九个一维数组,这些数组表示 0-9 之间的数字矩阵。我没有写出所有从零到九的数字,但它们的大小都相同,唯一的区别是 1 和 0 的位置。这部分是成功的,一旦我打印了 InitializeMatrices() 的结果,我确实得到了我想要的结果。 matrices[0][34] 将打印 "zero" 矩阵的第 35 个元素,matrice[1][2] 将打印"one" 矩阵的第 3 个元素等等。

问题开始于 DistortMatrices() 我试图在 matrixToLearn 中制作矩阵的副本(36 个副本,其中索引 0-17 将是零矩阵的副本),所以我将修改单个副本矩阵而不是矩阵本身并将更改存储在 matrixToLearn 中。可悲的是,它似乎并没有像我想象的那样工作。例如

matrixToLearn[0] = matrices[0]; // it copies the array zero as intended

matrixToLearn[0][34] = 0; //It applies to matrices[0][34] as well as all the 18 copies of matrixToLearn, even if after that i call:

matrixToLearn[17][7] = 0 //it will apply to matrices[0][34], matrixToLearn[0][34] and all the copies will be the same

所以它看起来像某种指针或 smth。

我该如何修复它,以便它将矩阵 [0] 复制到 matrixtoLearn[0-17],而对 matrixToLearn[0-17] 的更改将仅应用于自身及其对应的索引,并将矩阵保留为这是?看看--->代码下的预期结果

public partial class Form1 : Form
{

    int[] zero, one, two, three, four, five, six, seven, eight, nine;

    int[][] matrices;
    int[][] matrixToLearn = new int[36][];


    private void InitializeMatrices()
    {
        zero =
        new int[] {
            1, 1, 1, 1, 1,
            1, 0, 0, 0, 1,
            1, 0, 0, 0, 1,
            1, 0, 0, 0, 1,
            1, 0, 0, 0, 1,
            1, 0, 0, 0, 1,
            1, 1, 1, 1, 1,
        };

        //And so on up to nine

        matrices = new int[][]{ zero, one, two, three, four, five, six, seven, eight, nine};
    }

    //index = 0
    private void DistortMatrices(int index)
    {
        int z = 0;
        for (; z < 18; z++)
        {
            matrixToLearn[z] = matrices[index];
        }
        matrixToLearn[0][34] = 0;
        matrixToLearn[17][7] = 1;
    }
}

预期结果

Expected result

            matrixToLearn[0]
            1, 1, 1, 1, 1,
            1, 0, 0, 0, 1,
            1, 0, 0, 0, 1,
            1, 0, 0, 0, 1,
            1, 0, 0, 0, 1,
            1, 0, 0, 0, 1,
            1, 1, 1, 1, 0,

            matrixToLearn[17] 
            1, 1, 1, 1, 1,
            1, 0, 1, 0, 1,
            1, 0, 0, 0, 1,
            1, 0, 0, 0, 1,
            1, 0, 0, 0, 1,
            1, 0, 0, 0, 1,
            1, 1, 1, 1, 1,

            matrice[0] 
            1, 1, 1, 1, 1,
            1, 0, 0, 0, 1,
            1, 0, 0, 0, 1,
            1, 0, 0, 0, 1,
            1, 0, 0, 0, 1,
            1, 0, 0, 0, 1,
            1, 1, 1, 1, 1,

How it looks right now:

            matrixToLearn[0]
            1, 1, 1, 1, 1,
            1, 0, 1, 0, 1,
            1, 0, 0, 0, 1,
            1, 0, 0, 0, 1,
            1, 0, 0, 0, 1,
            1, 0, 0, 0, 1,
            1, 1, 1, 1, 0,

            matrixToLearn[17]
            1, 1, 1, 1, 1,
            1, 0, 1, 0, 1,
            1, 0, 0, 0, 1,
            1, 0, 0, 0, 1,
            1, 0, 0, 0, 1,
            1, 0, 0, 0, 1,
            1, 1, 1, 1, 0,

            matrices[0]
            1, 1, 1, 1, 1,
            1, 0, 1, 0, 1,
            1, 0, 0, 0, 1,
            1, 0, 0, 0, 1,
            1, 0, 0, 0, 1,
            1, 0, 0, 0, 1,
            1, 1, 1, 1, 0,

编辑:它似乎走得更远,它也修改了 "zero" 矩阵

matrixToLearn[0] = matrices[0];

这会将引用复制到嵌套数组。就像

float[] x = ...;
float[] y = x; //copies the reference, not the object contents

我通常使用 (float[])myArray.Clone() 来克隆阵列。我把它变成了一个小的通用扩展方法。