简体   繁体   English

C# 如何从具有变化的行大小的二维数组中获取行长度

[英]C# How to get row length from a 2D array with changing row sizes

I have a 2 dimensional array with rows that change in length.我有一个二维数组,其中行的长度会发生变化。 But with.GetLength(1) i only get the length of the first row in the second dimension.但是 with.GetLength(1) 我只能得到第二维第一行的长度。

How can i get the size of any specific row in an array?如何获取数组中任何特定行的大小?

What you are describing sounds more like a jagged array (array of arrays).您所描述的听起来更像是一个锯齿状数组(数组数组)。 That would look like this:看起来像这样:

int rowLength = My2dArray[rowIndex].Length;

If you really have a 2D array, then all the rows are required to be the same size, which is set when you first allocate the array.如果你真的有一个二维数组,那么所有的行都必须是相同的大小,这是在你第一次分配数组时设置的。 On the other hand, if you do have a jagged array then .GetLength(1) will throw an exception ;另一方面,如果您确实有一个锯齿状数组,那么.GetLength(1)将引发异常 it does not return the length of the first row as claimed.它不会像声称的那样返回第一行的长度。

Of course, this confusion could be avoided if you showed us how the collection is created and populated.当然,如果您向我们展示了如何创建和填充集合,则可以避免这种混淆。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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