简体   繁体   English

二维数组

[英]Two-dimensional array

I am using Visual Studio 2010 with Visual Basic. 我使用Visual Studio 2010与Visual Basic。 I have a dynamic 2 dimensional array that is coming into the function. 我有一个动态二维数组进入函数。 I cannot figure out how to get the size of first row. 我无法弄清楚如何获得第一行的大小。

For example let say this array is passe into the function: 例如,假设这个数组传入函数:

{{1, 0, 5, 4},
 {8, 1, 4, 4},
 {0, 1, 4, 4},
 {7, 7, 7, 4},
 {7, 7, 7, 4},
 {8, 1, 4, 4}}

In this example I would get 4 because if you look at for example first row it has 4 elements. 在这个例子中我会得到4,因为如果你看例如第一行它有4个元素。 And for example in this case: 例如,在这种情况下:

{{1, 0, 5, 4, 7, 7},
 {8, 1, 4, 4, 7, 7},
 {0, 1, 4, 4, 8, 8},
 {7, 7, 7, 4, 3, 3},
 {7, 7, 7, 4, 4, 4},
 {8, 1, 4, 4, 1, 9}}

I would get back 6 cause rows have 6 elements in it. 我会回来6因为行中有6个元素。 The array is coming in always 2d and rows are always the same length. 数组总是以2d进入,行总是相同的长度。 Column size is unknown and row size in unknown. 列大小未知且行大小未知。 But if I find out row size for one row the all rows are that size if that makes sense. 但是,如果我发现一行的行大小,那么所有行都是那么大,如果这有意义的话。

I have tried this UBound(inArray, 1) but it does not work. 我试过这个UBound(inArray,1),但它不起作用。 Please help me figure this out. 请帮我解决这个问题。

Have a look at using Array.GetUpperBound Method 看看使用Array.GetUpperBound方法

Gets the upper bound of the specified dimension in the Array. 获取Array中指定维度的上限。

Dim i(,) As Integer = {{1, 0, 5, 4}, {8, 1, 4, 4}, {0, 1, 4, 4}, {7, 7, 7, 4}, {7, 7, 7, 4}, {8, 1, 4, 4}}
MsgBox(i.GetUpperBound(0)) 'first index
MsgBox(i.GetUpperBound(1)) 'second index
MsgBox(UBound(i, 1)) 'first index (UBOUND is 1-based)
MsgBox(UBound(i, 2)) 'second index (UBOUND is 1-based)

For the 2D array i(x,y) , GetUpperBound(0) returns the maximum value of x , and GetUpperBound(1) returns the maximum value of y 对于2D数组i(x,y)GetUpperBound(0)返回x的最大值, GetUpperBound(1)返回y的最大值

To find the length of the x-axis, use the GetLength function. 要查找x轴的长度,请使用GetLength函数。 I have tried the GetUpperBound function and sometimes it has proven to be a little unreliable. 我已经尝试过GetUpperBound函数,有时它已被证明有点不可靠。 Please use the code below as reference: 请使用以下代码作为参考:

Dim array(1,10) As Double
Dim x as Integer = array.GetLength(0) - 1 'Will give you value of dimension x'

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

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