如何:确定一维数组的长度

更新:2007 年 11 月

数组的 GetLength 方法返回指定维的长度。

确定数组中某一维的长度

  • 对数组名称调用 GetLength。提供要获得其长度的维,作为 GetLength 的参数。注意维参数是从零开始的。

    Dim sampleTripleArray(,,) As Short = New Short(2, 3, 4) {} 
    MsgBox("Dimension lengths of sampleTripleArray are " & CStr(sampleTripleArray.GetLength(0)) _
        & ", " & CStr(sampleTripleArray.GetLength(1)) & ", " & CStr(sampleTripleArray.GetLength(2)))
    

    MsgBox 调用显示“Dimension lengths of sampleTripleArray are 3, 4, 5”。

每个维的最小索引值总是为 0,而 GetUpperBound 方法返回维的最大索引值。对于每个维,GetLength 的返回值都比 GetUpperBound 的返回值大 1。与 GetLength 一样,为 GetUpperBound 指定的维也是从零开始的。

可以使用数组的 Length 属性确定其总长度。

可以通过更改单个维的大小来更改数组的总大小。但是,不能更改秩(维数)。

请参见

任务

如何:声明数组变量

如何:创建数组

如何:初始化数组变量

如何:确定数组的大小

如何:更改数组的大小

数组疑难解答

概念

Visual Basic 中的数组大小

其他资源

数组 (Visual Basic)