简体   繁体   中英

Multidimensional array init without pre-existing values?

How can we initialize a multidimensional array without pre-existing values? Only the third one is correct, but it works with pre-existing values. I would like my multidimensional array to contain 10 or 20 values, and add them later on with numbers[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;

Would you know how to do this? (I don't know if [,] is the same as [][] by the way)

Thanks

You can try initiating values this way, it is one way to create 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;

Would you know how to do this? (I don't know if [,] is the same as [][] by the way) Would you know how to do this? (I don't know if [,] is the same as [][] by the way) there are not the some as int[][] test = new int[10][]; is called a jagged array (array of arrays) and int[,] is a fixed array

just declare your array as the following

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM