如何在C#中部分初始化3D数组?

时间:2017-11-19 03:48:10

标签: c# arrays initialization

我需要初始化一个3级数组,我知道2个第一级的大小,但最后一个是随机确定的,因数组而异。

int[,,] myArray;

如何初始化2个第一级但保持最后一个级别不变?

myArray = new int[sizeA, sizeB, ???];

1 个答案:

答案 0 :(得分:3)

,例如,数组3 x 5 x Variable_size:

        int[][][] myArray = new int[3][][];
        for (int i = 0; i < 3; i++)
            myArray[i] = new int[5][];

        myArray[0][0] = new int[5];
        myArray[0][1] = new int[6];
        myArray[0][2] = new int[3];
        myArray[0][3] = new int[2];
        myArray[0][4] = new int[7];
        myArray[1][0] = new int[3];
        //...
        myArray[2][4] = new int[9];

        int[,][] myArray = new int[3,5][];
        myArray[0, 0] = new int[3];
        myArray[0, 1] = new int[5];
        myArray[0, 2] = new int[2];
        myArray[0, 3] = new int[4];
        myArray[0, 4] = new int[6];
        myArray[1, 0] = new int[3];
        //...
        myArray[2, 4] = new int[8];

相应的用法:

myArray[0][1][3] = 5;
int a = myArray[1][0][2];

myArray[0, 1][3] = 5;
int a = myArray[1, 0][2];