没有预先存在的值的多维数组init?

时间:2013-12-09 16:58:43

标签: c# list multidimensional-array initialization

如何在没有预先存在的值的情况下初始化多维数组?只有第三个是正确的,但它适用于预先存在的值。我希望我的多维数组包含10或20个值,稍后使用数字[y] [x]添加它们:

int[][] numbers = new int[10][];
//List<int[]> numbers = new List<int[]>();
//int[10][]{ new int[]{}};
//correct :  { new int[] {1, 2}, new int[] {3, 4, 5} };
numbers[0][0] = 58;

你知道怎么做吗? (顺便说一句,我不知道[,]是否与[][]相同)

由于

2 个答案:

答案 0 :(得分:1)

您可以尝试以这种方式启动值,这是创建jagged-array

的一种方法
int[][] test = new int[10][];
test[0] = new int[20];
test[1] = new int[30];
test[2] = new[] { 3, 4, 5 };

test[0][1] = 10;
test[1][2] = 20;

答案 1 :(得分:1)

Would you know how to do this? (I don't know if [,] is the same as [][] by the way) 没有一些因为int[][] test = new int[10][];被称为jagged array(数组数组)而int [,]是固定数组

只需将您的数组声明为以下

 int[,] test = new int[10,30];    
test[0,1] = 10;
test[1,2] = 20;