简体   繁体   中英

Two-dimensional array

I am using Visual Studio 2010 with 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. 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. The array is coming in always 2d and rows are always the same length. 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. Please help me figure this out.

Have a look at using Array.GetUpperBound Method

Gets the upper bound of the specified dimension in the 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

To find the length of the x-axis, use the GetLength function. I have tried the GetUpperBound function and sometimes it has proven to be a little unreliable. 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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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