获取C#中多维数组的行/列的长度

时间:2020-12-03 21:35:38

How do I get the length of a row or column of a multidimensional array in C#?

如何在C#中获取多维数组的行或列的长度?

for example:

例如:

int[,] matrix = new int[2,3];

matrix.rowLength = 3;
matrix.colLength = 2;

3 个解决方案

#1


93  

matrix.GetLength(0)  -> Gets the first dimension size

matrix.GetLength(1)  -> Gets the second dimension size

#2


16  

Have you looked at the properties of an Array?

你看过数组的属性了吗?

  • Length gives you the length of the array (total number of cells).
  • 长度为您提供数组的长度(单元格总数)。
  • GetLength(n) gives you the number of cells in the specified dimension (relative to 0). If you have a 3-dimensional array:

    GetLength(n)为您提供指定维度(相对于0)的单元格数。如果你有一个三维数组:

    int[,,] multiDimensionalArray = new int[21,72,103] ;
    

    then multiDimensionalArray.GetLength(n) will, for n = 0, 1 and 2, return 21, 72 and 103 respectively.

    然后,对于n = 0,1和2,multiDimensionalArray.GetLength(n)将分别返回21,72和103。

If you're constructing Jagged/sparse arrays, then the problem is somewhat more complicated. Jagged/sparse arrays are [usually] constructed as a nested collection of arrays within arrays. In which case you need to examine each element in turn. These are usually nested 1-dimensional arrays, but there is not reason you couldn't have, say, a 2d array containing 3d arrays containing 5d arrays.

如果您正在构建Jagged /稀疏数组,那么问题会更复杂一些。锯齿状/稀疏数组[通常]构造为数组中数组的嵌套集合。在这种情况下,您需要依次检查每个元素。这些通常是嵌套的一维数组,但没有理由说你不能拥有包含含有5d数组的3d数组的二维数组。

In any case, with a jagged/sparse structure, you need to use the length properties on each cell.

在任何情况下,使用锯齿状/稀疏结构,您需要在每个单元格上使用长度属性。

#3


0  

Use matrix.GetLowerBound(0) and matrix.GetUpperBound(0).

使用matrix.GetLowerBound(0)和matrix.GetUpperBound(0)。

#1


93  

matrix.GetLength(0)  -> Gets the first dimension size

matrix.GetLength(1)  -> Gets the second dimension size

#2


16  

Have you looked at the properties of an Array?

你看过数组的属性了吗?

  • Length gives you the length of the array (total number of cells).
  • 长度为您提供数组的长度(单元格总数)。
  • GetLength(n) gives you the number of cells in the specified dimension (relative to 0). If you have a 3-dimensional array:

    GetLength(n)为您提供指定维度(相对于0)的单元格数。如果你有一个三维数组:

    int[,,] multiDimensionalArray = new int[21,72,103] ;
    

    then multiDimensionalArray.GetLength(n) will, for n = 0, 1 and 2, return 21, 72 and 103 respectively.

    然后,对于n = 0,1和2,multiDimensionalArray.GetLength(n)将分别返回21,72和103。

If you're constructing Jagged/sparse arrays, then the problem is somewhat more complicated. Jagged/sparse arrays are [usually] constructed as a nested collection of arrays within arrays. In which case you need to examine each element in turn. These are usually nested 1-dimensional arrays, but there is not reason you couldn't have, say, a 2d array containing 3d arrays containing 5d arrays.

如果您正在构建Jagged /稀疏数组,那么问题会更复杂一些。锯齿状/稀疏数组[通常]构造为数组中数组的嵌套集合。在这种情况下,您需要依次检查每个元素。这些通常是嵌套的一维数组,但没有理由说你不能拥有包含含有5d数组的3d数组的二维数组。

In any case, with a jagged/sparse structure, you need to use the length properties on each cell.

在任何情况下,使用锯齿状/稀疏结构,您需要在每个单元格上使用长度属性。

#3


0  

Use matrix.GetLowerBound(0) and matrix.GetUpperBound(0).

使用matrix.GetLowerBound(0)和matrix.GetUpperBound(0)。