vb.net中的数据结构类型——数组

2025-10-07 03:25:12

1、数组的定义:

Dim strName(9) As String

Private Sub btnArrayElement_Click(sender As Object,

                e As EventArgs) Handles btnArrayElement.Click

        'Clear the list

        ClearList()

        'Declare an array

        Dim strFriends(4) As String

        'Populate the array

        strFriends(0) = "Wendy"

        strFriends(1) = "Harriet"

        strFriends(2) = "Jay"

        strFriends(3) = "Michelle"

        strFriends(4) = "Richard"

        'Add the first array item to the list

        lstFriends.Items.Add(strFriends(0))

    End Sub

'Now create the following procedure:

Private Sub ClearList()

    'Clear the list

    lstFriends.Items.Clear()

End Sub

2、'Declare an array

Dim strFriends(4) As String

'Populate the array

strFriends(0) = "Wendy"

strFriends(1) = "Harriet"

strFriends(2) = "Jay"

strFriends(3) = "Michelle"

strFriends(4) = "Richard"

'Add the first array item to the list

lstFriends.Items.Add(strFriends(0))

3、窗体界面如下:

vb.net中的数据结构类型——数组

1、代码如下:

Public Class Form1

    'Declare a form level array

    Private strFriends(4) As String

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load

    'Populate the array

    strFriends(0) = "Wendy"

    strFriends(1) = "Harriet"

    strFriends(2) = "Jay"

    strFriends(3) = "Michelle"

    strFriends(4) = "Richard"

End Sub

Private Sub btnEnumerateArray_Click(sender As Object,

                e As EventArgs) Handles btnEnumerateArray.Click

    'Clear the list

    ClearList()

    'Enumerate the array

    For Each strName As String In strFriends

        'Add the array item to the list

        lstFriends.Items.Add(strName)

    Next

End Sub

end class

2、执行结果如下:

vb.net中的数据结构类型——数组

1、示例代码:

Private Sub btnArraysAsParameters_Click(sender As Object,

                e As EventArgs) Handles btnArraysAsParameters.Click

     'Clear the list

    ClearList()

    'List your friends

    AddItemsToList(strFriends)

End Sub

'

2、其中,我们需要先定义函数,再执行上述代码,函数如下:

Private Sub AddItemsToList(ByVal arrayList As String())

    'Enumerate the array

    For Each strName As String In arrayList

        'Add the array item to the list

        lstFriends.Items.Add(strName)

    Next

End Sub

其中要注意的是,该函数的参数是数组,arraylist作为参数。

1、在上面的基础上,我们再次添加成员

Private Sub btnMoreArrayParameters_Click(sender As Object,

                e As EventArgs) Handles btnMoreArrayParameters.Click

    'Clear the list

    ClearList()

    'Declare an array

    Dim strMoreFriends(1) As String

    'Populate the array

    strMoreFriends(0) = "Elaine"

    strMoreFriends(1) = "Debra"

    'List your friends

    AddItemsToList(strFriends)

    AddItemsToList(strMoreFriends)

End Sub

2、执行结果如下图所示:

vb.net中的数据结构类型——数组

1、下面的 程序是正向排序:

Private Sub btnSortingArrays_Click(sender As Object,

            e As EventArgs) Handles btnSortingArrays.Click

    'Clear the list

    ClearList()

    'Sort the array

    Array.Sort(strFriends)

    'List your friends

    AddItemsToList(strFriends)

End Sub

2、如何反向排序呢,反向排序有两种方法:

其一:(数组索引)

For intIndex As Integer = strFriends.GetUpperBound(0) To 0 Step -1    

        'Add the array item to the list    

         lstFriends.Items.Add(strFriends(intIndex)) 

Next

其二:(数组函数reverse)

Private Sub btnReversingAnArray_Click(sender As Object,

            e As EventArgs) Handles btnReversingAnArray.Click

    'Clear the list

    ClearList()

    'Reverse the order—elements will be in descending order

    Array.Reverse(strFriends)

    'List your friends

    AddItemsToList(strFriends)

End Sub

3、执行结果如下图所示;

vb.net中的数据结构类型——数组

1、Private Sub btnInitializingArraysWithValues_Click(sender As Object,

            e As EventArgs) Handles btnInitializingArraysWithValues.Click

    'Clear the list

    ClearList()

    'Declare and populate an array

    Dim strMyFriends() As String = {"Elaine", "Richard", "Debra",

        "Wendy", "Harriet"}

    'List your friends

    AddItemsToList(strMyFriends)

End Sub

2、执行结果和上图相同

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢