Set myList = new ArrayList
Dim e : e = Array(1,2,3,4)
myList.addAll e
call myList.setValue(0, 5)
MsgBox myList.getValue(0)
Class ArrayList
'实际用动态数组存储
Private elementsData()
'当前列表元素个数
Public count
'当前列表最大容量
Public maxSize
Dim i
'添加一个元素
Public Sub add(item)
'自动扩容
if count + 1 > maxSize then
maxSize = (maxSize + 1) * 1.5
Redim Preserve elementsData(maxSize)
end if
elementsData(count) = item
count = count + 1
End Sub
Public Sub addAll(items)
'调用单个添加的方法
Dim itemIndex : itemIndex = 0
for i = 0 to UBound(items)
Me.add(items(i))
next
End Sub
'按照下标删除某个元素
Public Sub remove(index)
for i = index to count - 1
elementsData(i) = elementsData(i + 1)
next
End Sub
'按照下标获取某个元素
Public Property get getValue(index)
getValue = elementsData(index)
End Property
'按下标设置元素
Public Sub setValue(index, elementValue)
elementsData(index) = elementValue
End Sub
'判断是否包含某个元素
Public Property get contains(item)
for i = 0 to count - 1
if elementsData(i) = item then
contains = True
exit Property
end if
next
contains = False
End Property
'判断是否包含某个元素
Public Sub clear()
for i = 0 to count - 1
elementsData(i) = Empty
next
count = 0
maxSize = 0
Redim Preserve elementsData(maxSize)
End Sub
'获取数组实际内容
Public Property get toArray()
toArray = elementsData
End Property
End Class
vbs实现ArrayList的数据结构
最新推荐文章于 2022-09-12 11:02:18 发布