2012-10-31 221 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'