ZetCode

Visual Basic 数组

最后修改于 2023 年 10 月 18 日

在本 Visual Basic 编程教程中,我们将介绍数组。

数组是数据的集合。一个变量一次只能保存一个项目。数组可以保存多个项目。这些项目称为数组的元素。数组存储相同数据类型的数据。每个元素都可以通过索引来引用。数组是基于零的。第一个元素的索引是零。

集合的作用类似。它们比数组更强大。稍后将进行描述。

数组用于存储应用程序的数据。我们声明数组具有特定的数据类型。我们指定它们的长度。我们使用数据初始化数组。我们有几种使用数组的方法。我们可以修改元素,对它们进行排序、复制或搜索。

初始化数组

在 Visual Basic 中,有几种初始化数组的方法。

Program.vb
Option Strict On

Module Example

    Sub Main()

        Dim array(5) As Integer

        array(0) = 3
        array(1) = 2
        array(2) = 1
        array(3) = 5
        array(4) = 6

        For i As Integer = 0 To array.Length-1
            Console.WriteLine(array(i))
        Next

    End Sub

End Module

我们声明并初始化一个数值数组。数组的内容将打印到控制台。

Dim array(5) As Integer

在这里,我们声明一个包含五个元素的数组。所有元素都是整数。

array(0) = 3
array(1) = 2
...

我们使用一些数据初始化数组。这是赋值初始化。索引在括号中。数字 3 将是数组的第一个元素,2 是第二个元素。

For i As Integer = 0 To array.Length-1
    Console.WriteLine(array(i))
Next

我们遍历数组并打印其元素。数组有一个 Length 属性,它给出数组中元素的数量。由于数组是基于零的,因此索引为 0..length-1。

我们可以在一条语句中声明和初始化数组。

Program.vb
Option Strict On

Module Example

    Sub Main()

        Dim array() As Integer = { 2, 4, 5, 6, 7, 3, 2 }

        For Each i As Integer In array
            Console.WriteLine(i)
        Next

    End Sub

End Module

这是上一个程序的修改版本。

Dim array() As Integer = { 2, 4, 5, 6, 7, 3, 2 }

数组在一个步骤中声明和初始化。元素在花括号中指定。我们没有指定数组的长度。编译器将为我们完成。

For Each i As Integer In array
    Console.WriteLine(i)
Next

我们使用 For Each 关键字遍历数组并打印其内容。

数组的边界

Visual Basic 有两个用于获取数组边界的函数。LBound 函数返回数组指示维度的最低可用下标。UBound 函数返回数组指示维度的最高可用下标。到目前为止,我们已经使用了单维数组。

Program.vb
Option Strict On

Module Example

    Dim n1 As Integer
    Dim n2 As Integer

    Sub Main()

        Dim names() As String = { "Jane", "Lucy", "Timea", "Beky", "Lenka"}

        n1 = LBound(names)
        n2 = UBound(names)

        Console.WriteLine(n1)
        Console.WriteLine(n2)

        For i As Integer = n1 To n2
            Console.WriteLine(names(i))
        Next

    End Sub

End Module

我们有一个名称数组。我们计算并处理该数组的下限和上限。

n1 = LBound(names)
n2 = UBound(names)

n1 是最低索引,n2 是名称数组中的最高索引。

For i As Integer = n1 To n2
    Console.WriteLine(names(i))
Next

我们使用数组的下限和上限来检查数组。

$ dotnet run
0
4
Jane
Lucy
Timea
Beky
Lenka

数组维度

到目前为止,我们已经使用了单维数组。指定元素所需的索引数称为数组的*维度*或*秩*。

我们使用二维数组。

Program.vb
Option Strict On

Module Example

    Sub Main()

        Dim numbers(,) As Integer = { {2, 1}, {3, 5}, 
            {4, 4}, {7, 2}, {0, 0} }

        For i As Integer = 0 To UBound(numbers, 1)
            For j As Integer = 0 To UBound(numbers, 2)
                Console.Write(CStr(numbers(i, j)) + " ")
            Next j
            Console.Write(vbCrLf)
        Next i

    End Sub

End Module

如果我们需要两个索引来访问数组中的一个元素,那么我们有一个二维数组。

Dim numbers(,) As Integer = { {2, 1}, {3, 5}, 
    {4, 4}, {7, 2}, {0, 0} }

我们在一个语句中声明并初始化一个二维数组。请注意数组名称后面的括号内的逗号。

For i As Integer = 0 To UBound(numbers, 1)
    For j As Integer = 0 To UBound(numbers, 2)
        Console.Write(CStr(numbers(i, j)) + " ")
    Next j
    Console.Write(vbCrLf)
Next i

我们需要两个循环才能从二维数组获取数据。UBound 函数有一个可选的第二个参数,rank。它是一个我们检索最高索引的维度。如果省略 rank,则假定为第一维。

$ dotnet run
2 1
3 5
4 4
7 2
0 0

接下来,我们使用一个三维数组。

Program.vb
Option Strict On

Module Example

    Sub Main()

        Dim i As Integer
        Dim j As Integer
        Dim k As Integer

        Dim nums(,,) As Integer = { 
            {{12, 2, 8}}, 
            {{14, 5, 2}}, 
            {{3, 26, 9}}, 
            {{4, 11, 2}} 
        }

        For i = 0 To UBound(nums, 1)
            For j = 0 To UBound(nums, 2)
                For k = 0 To UBound(nums, 3)
                    Console.Write(CStr(nums(i, j, k)) + " ")
                Next k
            Next j
            Console.Write(vbCrLf)
        Next i

    End Sub

End Module

我们有一个数值三维数组。同样,我们用数字初始化数组并将它们打印到终端。

Dim nums(,,) As Integer = { 
    {{12, 2, 8}}, 
    {{14, 5, 2}}, 
    {{3, 26, 9}}, 
    {{4, 11, 2}} 
}

左边的括号之间还有另一个逗号,右边还有额外的花括号。

For k = 0 To UBound(nums, 3)
    Console.Write(CStr(nums(i, j, k)) + " ")
Next k

此循环遍历第三个维度。我们使用三个索引从数组中检索值。

$ dotnet run
12 2 8
14 5 2
3 26 9
4 11 2

我们将三维数组的内容打印到控制台。

有一个 Rank 函数,它给出数组的维数。

Program.vb
Option Strict On

Module Example

    Sub Main()

        Dim array1() As Integer = {1, 2}
        Dim array2(,) As Integer = { { 1 }, { 2 } }
        Dim array3(, ,) As Integer = { { { 1, 2 }, { 2, 1 } } }

        Console.WriteLine(array1.Rank())
        Console.WriteLine(array2.Rank())
        Console.WriteLine(array3.Rank())

    End Sub

End Module

我们有三个数组。我们使用 Rank 函数来获取每个数组的维数。

Console.WriteLine(array1.Rank())

在这里,我们获取第一个数组的秩。

交错数组

具有相同大小元素的数组称为*矩形*数组。相反,具有不同大小元素的数组称为*交错*数组。交错数组的声明和初始化方式不同。

Program.vb
Option Strict On

Module Example

    Sub Main()

        Dim jagged As Integer()() = New Integer(4)() {}

        jagged(0) = New Integer() {1}
        jagged(1) = New Integer() {3, 4}
        jagged(2) = New Integer() {5, 6, 7}
        jagged(3) = New Integer() {5, 6}
        jagged(4) = New Integer() {9}


        For i As Integer = 0 To jagged.GetUpperBound(0)
            For j As Integer = 0 To jagged(i).GetUpperBound(0)
                Console.Write(jagged(i)(j) & " ")
            Next

            Console.Write(vbCrLf)
        Next

    End Sub

End Module

这是一个交错数组的示例。

Dim jagged As Integer()() = New Integer(4)() {}

这是交错数组的声明。我们有一个数组的数组。更具体地说,我们声明一个数组,使其具有五个 Integer 数据类型的数组。

jagged(0) = New Integer() {1}
jagged(1) = New Integer() {3, 4}
...

每个数组都必须单独初始化。

Console.Write(jagged(i)(j) & " ")

与矩形数组不同,每个索引都用括号括起来。

数组方法

有多种使用数组的方法。这些方法可用于检索、修改数据、排序、复制、搜索数据。我们使用的方法是 Array 类的静态方法或数组对象的成员方法。

Program.vb
Option Strict On

Module Example

    Sub Main()

        Dim names() As String = {"Jane", "Frank", "Alice", "Tom" }

        Array.Sort(names)

        For Each el As String In names
            Console.Write(el + " ")
        Next

        Console.Write(vbCrLf)

        Array.Reverse(names)

        For Each el As String In names
            Console.Write(el + " ")
        Next

        Console.Write(vbCrLf)

    End Sub

End Module

在此示例中,我们对数据进行排序。

Dim names() As String = {"Jane", "Frank", "Alice", "Tom" }

我们有一个字符串数组。

Array.Sort(names)

Sort 方法按字母顺序对数据进行排序。

Array.Reverse(names)

Reverse 方法反转整个一维数组中元素的顺序。

$ dotnet run
Alice Frank Jane Tom
Tom Jane Frank Alice

我们已将名称按升序和降序排序。

以下示例使用 SeValueGetValueIndexOfCopyClear 方法。

Program.vb
Option Strict On

Module Example

    Dim names() As String = {"Jane", "Frank", "Alice", "Tom" }
    Dim girls(0 To 3) As String

    Sub Main()

        names.SetValue("Beky", 1)
        names.SetValue("Erzebeth", 3)

        Console.WriteLine(names.GetValue(1))
        Console.WriteLine(names.GetValue(3))

        Console.WriteLine(Array.IndexOf(names, "Erzebeth"))

        Array.Copy(names, girls, names.Length)

        For Each el As String In girls
            Console.Write(el + " ")
        Next

        Console.Write(vbCrLf)

        Array.Clear(names, 0, 2)

        For Each el As String In names
            Console.Write(el + " ")
        Next

        Console.Write(vbCrLf)

    End Sub

End Module

此示例介绍了其他方法。

Dim girls(0 To 3) As String

另一种声明数组的方法。

names.SetValue("Beky", 1)
names.SetValue("Erzebeth", 3)

SetValue 设置数组中特定索引的值。

Console.WriteLine(names.GetValue(1))
Console.WriteLine(names.GetValue(3))

我们使用 GetValue 方法从数组中检索值。

Console.WriteLine(Array.IndexOf(names, "Erzebeth"))

IndexOf 方法返回特定值的第一次出现的索引。

Array.Copy(names, girls, names.Length)

Copy 方法将值从源数组复制到目标数组。第一个参数是源数组,第二个参数是目标数组。第三个参数是长度;它指定要复制的元素数。

Array.Clear(names, 0, 2)

Clear 方法从数组中清除元素。它需要三个参数:数组、起始索引和要清除的索引中的元素数。

在本 Visual Basic 教程中,我们使用了数组。