vb.net中的数据结构类型——数组
的有关信息介绍如下:通过英文的形式和大家分享经验,也有利于大家看懂老外的代码,原创经验,谢绝通过其他方式传播该经验!英文都是用简单的句子,大家如果认真看都可以看懂的!复杂的地方我用汉语注释!
数组的定义:
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
'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))
窗体界面如下:
代码如下:
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
执行结果如下:
示例代码:
Private Sub btnArraysAsParameters_Click(sender As Object,
e As EventArgs) Handles btnArraysAsParameters.Click
'Clear the list
ClearList()
'List your friends
AddItemsToList(strFriends)
End Sub
'
其中,我们需要先定义函数,再执行上述代码,函数如下:
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作为参数。
在上面的基础上,我们再次添加成员
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
执行结果如下图所示:
下面的 程序是正向排序:
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
如何反向排序呢,反向排序有两种方法:
其一:(数组索引)
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
执行结果如下图所示;
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
执行结果和上图相同