How do i initialize int[][]?
In C# how do i initialize
int[][] test;
I don't want to use int[,]
i开发者_如何学编程nstead. I am asking specifically how to initialize the above. The obvious = int[1][1]
does not work. I tried a few different ways with no luck and [][] is not really google-able unfortunately (unless its possible!?)
It's called jagged arrays if you want to Google it.
Basically you can initialize the first dimension in the traditional way :
int[][] test = new int[23][];
And you manually initialize the others :
for (int i = 0; i < test.Length; ++i)
test[i] = new int[42];
You need a for-loop to initialize an array of array.
int[][] test = new int[N][];
for (int i = 0; i < test.Length; i ++)
test[i] = new int [M];
int[][] scores = new int[5][];
This is called a Jagged Array. So basically you have initialize the "first dimension" first and then each element:
int[][] foo = new int[3][];
foo[0] = new int[2];
...
精彩评论