我需要初始化一个3级数组,我知道2个第一级的大小,但最后一个是随机确定的,因数组而异。
int[,,] myArray;
如何初始化2个第一级但保持最后一个级别不变?
myArray = new int[sizeA, sizeB, ???];
答案 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];