跳转至

{% raw %}

Visual Basic 数组

原文: https://zetcode.com/lang/visualbasic/arrays/

在 Visual Basic 编程教程的这一部分中,我们将介绍数组。 我们将初始化数组并从中读取数据。

数组是数据的集合。 变量一次只能容纳一项。 数组可以容纳多个项目。 这些项目称为数组的元素。 数组存储相同数据类型的数据。 每个元素都可以由索引引用。 数组从零开始。 第一个元素的索引为零。

集合具有相似的目的。 它们比数组更强大。 它们将在后面描述。

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

初始化数组

有几种方法可以在 Visual Basic 中初始化数组。

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

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

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()函数返回数组指定维的最高可用下标。 到目前为止,我们已经处理了一维数组。

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

我们使用数组的上下边界检查数组。

$ ./bounds.exe 
0
4
Jane
Lucy
Timea
Beky
Lenka

示例的输出。

数组大小

到目前为止,我们已经处理了一维数组。 指定元素所需的索引数称为数组的维或等级。

我们将使用二维数组。

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(vbNewLine)
        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(vbNewLine)
Next i

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

$ ./twodimensions.exe 
2 1 
3 5 
4 4 
7 2 
0 0 

代码示例的输出。

接下来,我们将处理三维数组。

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(vbNewLine)       
        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

该循环经过三维。 我们使用三个索引从数组中检索值。

$ ./3darray.exe 
12 2 8 
14 5 2 
3 26 9 
4 11 2 

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

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

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

在这里,我们获得第一个数组的排名。

锯齿状数组

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

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(vbNewLine)
        Next

    End Sub

End Module

这是一个锯齿状数组的示例。

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

这是锯齿状数组的声明。 我们有一个数组数组。 更具体地说,我们声明了一个数组,该数组具有五个整数数据类型的数组。

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

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

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

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

数组方法

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

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(vbNewLine)

        Array.Reverse(names)

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

        Console.Write(vbNewLine)

    End Sub

End Module

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

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

我们有一个字符串数组。

Array.Sort(names)

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

Array.Reverse(names)

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

$ ./sorting.exe 
Alice Frank Jane Tom 
Tom Jane Frank Alice

我们已经按升序和降序对名称进行了排序。

以下示例使用SeValue()GetValue()IndexOf()Copy()Clear()方法。

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(vbNewLine)

        Array.Clear(names, 0, 2)

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

        Console.Write(vbNewLine)

    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 教程的这一部分中,我们使用了数组。 我们描述了各种类型的数组和使用它们的方法。

{% endraw %}


我们一直在努力

apachecn/AiLearning

【布客】中文翻译组