Iterate through 2 dimensional array c#

后端 未结 7 1507
悲&欢浪女
悲&欢浪女 2020-12-03 16:37
for(int k=0;k <= odds.GetLength(-1);k++)

The above line of code is supposed to iterate through a two dimensional array of type Double but keeps

7条回答
  •  抹茶落季
    2020-12-03 17:30

    You are passing an invalid index to GetLength. The dimensions of a multidimensional array are 0 based, so -1 is invalid and using a negative number (or a number that is larger than the number of dimensions - 1) would cause an IndexOutOfRangeException.

    This will loop over the first dimension:

    for (int k = 0; k < odds.GetLength(0); k++)
    

    You need to add another loop to go through the second dimension:

    for (int k = 0; k < odds.GetLength(0); k++)
        for (int l = 0; l < odds.GetLength(1); l++)
            var val = odds[k, l];
    

提交回复
热议问题