2012-10-31 67 views
1

我在Visual Basic中使用Visual Studio 2010。我有一個動態的二維數組,正在進入函數。我無法弄清楚如何獲得第一排的大小。二維數組

例如,讓說,這陣是過時進入功能:

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

在這個例子中,我會得到4因爲如果你看一下例如第一行有4個元素。例如在這種情況下:

{{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}} 

我會回來6因爲行中有6個元素。數組總是以2d進來,行總是相同的長度。列大小未知,行大小未知。但是如果我發現一行的行大小,那麼所有的行都是這個大小,如果這是合理的。

我試過這個UBound(inArray,1),但它不起作用。請幫我弄清楚這一點。

回答

2

看一看使用Array.GetUpperBound Method

獲取上限在數組中指定的尺寸。

+1

MSGBOX(「連續元件的數量,」 + inArray.GetUpperBound(1)的ToString),其工作謝謝:)我想我需要休息一下。 :) –

1
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) 

對於2D陣列i(x,y)GetUpperBound(0)返回x的最大值,和GetUpperBound(1)回報y

+0

感謝大家現在工作:) –

0

最大值爲了找到x軸的長度,則使用對GetLength函數。我嘗試過GetUpperBound函數,有時證明它有點不可靠。請使用下面的代碼作爲參考:

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