一、概述
VB Mscorlib组件中 ArrayList数据结构,是系统中集合对象中一个功能相对强大的数据结构对象,可以认为是Array的升级版本,像集合一样可以单独索引的对象的有序序列。可以用 索引 在指定位置添加和删除列表中的项,并且数组自动调整大小,支持动态内存分配,添加。VB中,ArrayList提供了丰富的功能,包括排序、数组转换、动态的增加和减少元素、删除所有元素项目等。相比字典、数据、集合,ArrayList主要优势:可以写入重复的item,并可查找;可以在指定索引位置插入item;可以选择对item进行排序或不排序(包括去重排序);可以通过item或索引删除(排序前排序后都可以),效率问题在后期测试对比说明。
二、ArrayList的基本属性
1.创建ArrayList数据结构对象
(1)前期引用引用Mscomlib.dll
(2)后期绑定
Dim ArrList: Set ArrList = CreateObject("System.Collections.ArrayList")
2.添加或插入数据item
Add方法,arrList.Add item
其中itme 可以是数字,字符串,对象等变量
Insert方法,arrList.insert index,item 在特定位置插入数据
3.查找或删除item(可清空所有数据)
ArrayList.Clear 数据全部清空
删除方式一:
ArrayList.Remove item
其中,item必须是存在的,否则删除无效,且无错误。
删除方式二
ArrayList.removeAt index
其中,index必须存在,否则出错
查找:
ArrayList.Contains(item) ,返回True or False
获取值:
ArrayList.item(index)
4.排序
ArrayList.sort 排序,默认一般按升序,数据类型数字或字符串均可
ArrayList.reverse 反序
5.复制
ArrayList.clone 创建一个新的副本对象
6.输出(可转数组)
ArrayList.toArray
7.释放(删除)arrList
ArrayList.Clear
三、ArrayList的使用范例
1.初始化对象
'Dim ArrList: Set ArrList = CreateObject("System.Collections.ArrayList")
Dim AL AS NEW ArrayList
2.添加数据
Private Sub Command1_Click()
'添加数据
Dim arr1: arr1 = Array("秦岭2", "秦岭1", "秦岭1", "大雁塔1", "大雁塔1", "钟楼1", "钟楼1", "杭州2", "杭州2")
Dim d
Me.List1.Clear
For Each d In arr1
Me.List1.AddItem d
AL.Add d
Next
Me.List2.Clear
For Each d In AL.toArray
Me.List2.AddItem d
Next
End Sub
3.插入数据
Private Sub Command2_Click()
'插入数据
Dim index As Long
Dim sItem As String
index = CLng(Me.Text1)
sItem = Me.Text2
AL.Insert index, sItem
Me.List3.Clear
For Each d In AL.toArray
Me.List3.AddItem d
Next
End Sub
4.删除数据
Private Sub Command3_Click()
'删除方式一
Dim sItem As String
sItem = Me.Text2
AL.Remove sItem
Me.List4.Clear
For Each d In AL.toArray
Me.List4.AddItem d
Next
End Sub
Private Sub Command4_Click()
'删除方式二
AL.RemoveAt CLng(Me.Text1)
For Each d In AL.toArray
Me.List4.AddItem d
Next
End Sub
5.查找数据
Private Sub Command5_Click()
'查找
Dim sItem As String
sItem = Me.Text2
Dim s As String
s = IIf(AL.Contains(sItem), "数据存在", "数据不存在")
MsgBox sItem & "查找结果:" & vbCrLf & s
End Sub
6.遍历数据
Private Sub Command6_Click()
'数据遍历,一般转化为数组遍历较为方便快捷
Me.List4.Clear
For i = 0 To AL.Count - 1
Me.List4.AddItem AL.Item(i)
Next
End Sub
7.排序数据
Private Sub Command7_Click()
'排序
AL.sort
Me.List4.Clear
For Each d In AL.toArray
Me.List4.AddItem d
Next
End Sub
8.反向排序数据
Private Sub Command8_Click()
'反序
AL.reverse
Me.List3.Clear
For Each d In AL.toArray
Me.List3.AddItem d
Next
End Sub
9.复制数据
Private Sub Command9_Click()
'复制
Dim al1
Set al1 = AL.Clone
MsgBox "新副本数据量为:" & vbCrLf & al1.Count
End Sub
10.转化为数组数据
Private Sub Command10_Click()
'转数组
Dim arr
arr = AL.toArray
End Sub
11.去重
Private Sub Command11_Click()
'去重
Dim al2 As New ArrayList
For Each d In AL.toArray
If Not al2.Contains(d) Then
al2.Add d
End If
Next
Me.List4.Clear
For Each d In al2.toArray
Me.List4.AddItem d
Next
End Sub
12.自定义功能扩展
(1)排序函数,可选升序、降序,默认升序
Private Sub Command12_Click()
'降序排列
Dim arr
arr = Array(1, 3, 2, 4, 7, 5, 4, 3, 2, 3, 4, 6, 9, 1, 22, 4, 111, 55, 33, 66, 33, 22, 77, 99, 6, 4, 3, 2)
Dim s1 As String, s2 As String
s1 = Join(arr, ",")
s2 = Join(SortArray(arr), ",")
MsgBox "原始数据:" & s1 & vbCrLf & "排序数据:" & s2
End Sub
'数据排序
Private Function SortArray(ByRef arr As Variant, Optional SortByASC As Boolean = True) As Variant
'函数功能:数组排序
'参数: arr --->排序的原始数组
' byASC--->,可选参数,是否升序排序,默认值升序
'返回:
' 排序后的新数组,使用变体类型接收
Dim arrList As Object
Dim d As Variant
Set arrList = CreateObject("System.Collections.ArrayList")
For Each d In arr
arrList.Add d
Next
arrList.sort
If Not SortByASC Then arrList.reverse
SortArray = arrList.toArray()
Set arrList = Nothing
End Function
(2)数组去重函数
Private Sub Command13_Click()
'去重
Dim arr
arr = Array(1, 3, 2, 4, 7, 5, 4, 3, 2, 3, 4, 6, 9, 1, 22, 4, 111, 55, 33, 66, 33, 22, 77, 99, 6, 4, 3, 2)
Dim s1 As String, s2 As String
MsgBox "原始数据:" & Join(arr, ",") & vbCrLf & "去重数据:" & Join(getDistinctArr(arr), ",")
End Sub
Public Function getDistinctArr(arr) As Variant
'函数功能:
' 数组去重
'参数: arr ---> 去重的数组
'
'返回: 去重后的数组
Dim arrList As Object
Dim d As Variant, arrNew As Variant
Set arrList = CreateObject("System.Collections.ArrayList")
For Each d In arr
If Not arrList.Contains(d) Then arrList.Add d
Next
getDistinctArr = arrList.toArray
Set arrList = Nothing
End Function
(3)数组查询
Private Sub Command14_Click()
'数据查询
Dim arr
arr = Array(1, 3, 2, 4, 7, 5, 4, 3, 2, 3, 4, 6, 9, 1, 22, 4, 111, 55, 33, 66, 33, 22, 77, 99, 6, 4, 3, 2)
Dim b As Boolean, s As String
Dim ele As Integer
ele = 4
b = ArrayContainsEle(arr, ele)
s = IIf(b, "元素存在!", "元素不存在!")
MsgBox ele & "查询状态:" & s
End Sub
Public Function ArrayContainsEle(ByRef arr As Variant, ele As Variant) As Boolean
'函数功能:判断数组是否存在某元素
'参数: arr --->判断的数组
' ele --->判断的元素
'返回:
' 真假值
Dim arrList As Object
Dim d As Variant, bHas As Boolean
Set arrList = CreateObject("System.Collections.ArrayList")
For Each d In arr
arrList.Add d
Next
bHas = arrList.Contains(ele)
Set arrList = Nothing
ArrayContainsEle = bHas
End Function
(4)数组反序
Private Sub Command16_Click()
'数组反序
Dim arr
arr = Array(1, 3, 2, 4, 7, 5, 4, 3, 2, 3, 4, 6, 9, 1, 22, 4, 111, 55, 33, 66, 33, 22, 77, 99, 6, 4, 3, 2)
MsgBox "原始数据:" & Join(arr, ",") & vbCrLf & "反序数据:" & Join(arrReverse(arr), ",")
End Sub
Private Function arrReverse(arr) As Variant
'函数功能: 数组反序
'参数:
' arr ---> 反序的操作数组
'返回:
' 反序后的新数组
Dim arrList As Object
Dim d As Variant, arrNew As Variant
Set arrList = CreateObject("System.Collections.ArrayList")
For Each d In arr
arrList.Add d
Next
arrList.reverse
arrReverse = arrList.toArray
Set arrList = Nothing
End Function
后记,ArrayList数据结构非常实用,同数组的转化较为方便、快捷,正、反排序较为实用,数据去重其实利用字典的(Dic(k)=0)特性更为迅速。国内VB语言现在最大的使用需求可能是其子集VBA语言在office的自动化办公领域;国外仍有一定比例的VB语言使用群体,集中于网站开发或出于成本考虑维护一些旧的VB开发的系统,也有相关比例的一些活跃站点论坛。国内VB.NET在网站开发方面的占比也不多,虽然VB.NET可能实现跨平台开发,但当前使用VB的群体,可能多是一些对VB语言有情怀的业余爱好者吧!
VB精巧,开发小工具快捷,新人上手快;调用测试验证C\C++一些逻辑算法也不错!可惜MS对VB6已经终止更新支持,实际上目前Win10仍然兼容,VB.NET且行且珍惜!Python目前地位颠峰,应用领域广泛参透,在Office自动化办公方面同vba也是狭路相逢,祝福Python。
世界万物,“反者也,道之动也,弱者也,道之用也”。