vb.net中的数据结构类型——数组
通过英文的形式和大家分享经验,也有利于大家看懂老外的代码,原创经验,谢绝通过其他方式传播该经验!英文都是用简单的句子,大家如果认真看都可以看懂的!复杂的地方我用汉语注释!
工具/原料
visual studio 2015
Using arrays
1、数盲褓梆尺组的定义:Dim strName(9) As StringPrivate Sub btnArrayEle罪焐芡拂ment_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 arrayDim strFriends(4) As String'Populate the arraystrFriends(0) = "Wendy"strFriends(1) = "Harriet"strFriends(2) = "Jay"strFriends(3) = "Michelle"strFriends(4) = "Richard"'Add the first array item to the listlstFriends.Items.Add(strFriends(0))
3、窗体界面如下:

数组作为参数传递给函数
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) NextEnd 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、执行结果如下图所示:

数组数值的初始化
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、执行结果和上图相同