VBA进阶:SortedList详解之操作

2019-12-24 15:40:52 浏览数 (1)

检查键和元素值

Contains属性

Contains属性返回True或False,表示出现指定的键。

With sl

Dim str

For Each str In Array("aa1", "aa2", "aa1","aa3", "aa4", "aa2")

If Not .Contains(str) Then .Add str, 60

Next str

MsgBox .Contains("aa4")

MsgBox .Contains("aa5")

End With

ContainsKey属性

ContainsKey属性返回True或False,表示是否包含指定的键。可以使用ContansKey属性防止值的替换或错误的产生。

With sl

Dim str

For Each str In Array("aa1", "aa2", "aa1","aa3", "aa4", "aa2")

If Not .ContainsKey(str) Then .Add str, 660

Next str

End With

ContainsValue属性

ContainsValue属性返回True或False,表示SortedList是否包含某个值。可以使用ContainsValue属性来防止将某个值连接到2个不同的键。

With sl

Dim str

For Each str In Array("aa1", "aa2", "aa1","aa3", "aa4", "aa2")

If Not .ContainsValue(str) Then .Add .Count, str

Next str

End With

检查键和元素的位置

IdexOfKey属性

IndexOfKey属性返回某个键的索引号。第一个键的索引编号为0。如果键不存在,则结果值为-1。

With sl

Dim str

For Each str In Array("aa1", "aa2", "aa3","aa4", "aa5", "aa6")

If Not .Contains(str) Then .Add str, 2 * .Count

Next str

MsgBox .IndexOfKey("aa3")

End With

IndexOfValue属性

IndexOfValue属性返回某个元素的索引号。第一个元素的索引号为0。如果元素不存在,则结果值为-1。

With sl

Dim str

For Each str In Array("aa1", "aa2", "aa3","aa4", "aa5", "aa6")

If Not .Contains(str) Then .Add str, 2 * .Count

Next str

MsgBox .IndexOfValue(6)

End With

获取元素

通过索引号获取元素:GetByIndex和GetValueList

可以使用索引号返回SortedList中的相应元素。SortedList中的第一项的索引号为0。

With sl

Dim str

For Each str In Array("aa1", "aa2", "aa3","aa4", "aa5", "aa6")

.Add str, 3 * .Count

Next str

MsgBox .GetByIndex(0)

MsgBox .GetByIndex(.Count - 1)

MsgBox .GetByIndex(3)

MsgBox .GetValueList()(0)

MsgBox .GetValueList()(.Count- 1)

MsgBox .GetValueList()(3)

End With

通过键获取元素:Item

Item方法基于键来获取相应的元素。

With sl

Dim str

For Each str In Array("aa1", "aa2", "aa3","aa4", "aa5", "aa6")

.Add str, 3 * .Count

Next str

MsgBox .Item("aa3")

End With

通过索引号获取相应的键:GetKey和GetKeyList

如果想知道哪个键在SortedList中具有的层级,可以使用方法:GetKey和GetKeyList。

With sl

Dim str

For Each str In Array("aa1", "aa2", "aa3","aa4", "aa5", "aa6")

.Add str, 3 * .Count

Next str

MsgBox .GetKey(2)

MsgBox .GetKeyList()(2)

End With

基于索引号获取所有元素:GetByIndex和GetValueList

ArrayList有一个自己的方法来检索所有元素:ToArray。字典也有这样一种方法:Items。但SortedList中没有这种方法。

在SortedList中,需要遍历来读取所有单独的元素。而GetByIndex方法和GetValueList方法是执行此操作的适当方法。结果是一个包含所有元素的列表,这些元素按其键升序排列。可以通过按降序索引号检索元素来获得降序。

With sl

Dim str, i, t00, t01, t02, t03

For Each str In Array("aa1", "aa2", "aa3","aa4", "aa5", "aa6")

.Add str, 3 * .Count

Next str

For i = 0 To .Count - 1

t00 = t00 & .GetByIndex(i)

t01 = t01 & .GetValueList()(i)

t02 = t02 & .GetByIndex(.Count - 1 - i)

t03 = t03 & .GetValueList()(.Count - 1 - i)

Next i

MsgBox t00

MsgBox t01

MsgBox t02

MsgBox t03

End With

获取所有键:GetKey和GetKeyList

字典提供了一种获取所有键的方法:Keys。在SortedList中,需要遍历来读取所有单独的键,而GetKey方法和GetKeyList方法是执行此操作的适当方法,结果是一个包含所有升序排序的键的列表。可以通过按降序索引号检索键来获得降序。

With sl

Dim str, i, t00, t01, t02, t03

For Each str In Array("aa1", "aa2", "aa3","aa4", "aa5", "aa6")

.Add .Count, str

Next str

For i = 0 To .Count - 1

t00 = t00 & vbLf & .GetKey(i)

t01 = t01 & vbLf & .GetKeyList()(i)

t02 = t02 & vbLf & .GetKey(.Count - 1 - i)

t03 = t03 & vbLf & .GetKeyList()(.Count - 1 - i)

Next i

MsgBox t00

MsgBox t01

MsgBox t02

MsgBox t03

End With

修改元素

通过键修改/替换元素:Item

可以通过赋新值或元素来修改/替换SortedList中的元素。如果知道键,则可以使用Item方法。

With sl

Dim str

For Each str In Array("aa1", "aa2", "aa3","aa4", "aa5", "aa6")

.Add str, str

Next str

MsgBox .Item("aa3")

.Item("aa3") = 123

MsgBox .Item("aa3")

End With

通过索引号修改/替换元素:SetByIndex

可以通过赋新值或元素来修改/替换SortedList中的元素。如果索引号已知,则可以使用SetByIndex方法。

With sl

Dim str

For Each str In Array("aa1", "aa2", "aa3","aa4", "aa5", "aa6")

.Add str, str

Next str

MsgBox .GetByIndex(3)

.SetByIndex(3) = "bb00"

MsgBox .GetByIndex(3)

End With

对元素排序

SortedList基于键对元素进行升序排序,如果键的类型不同(数字或字符串),则会发生错误。

按字符串排序

With sl

Dim str, i, t00

For Each str In Array("aa1", "aa2", "aa3","aa13", "aa27", "aa6")

.Add str, .Count

Next str

For i = 0 To .Count - 1

t00 = t00 & vbLf & .GetKey(i)

Next i

MsgBox t00

End With

按数字排序

With sl

Dim num, i, t00

For Each num In Array(23, 35, 12, 360, 320, 6)

.Add .Count, num

Next num

For i = 0 To .Count - 1

t00 = t00 & vbLf & .GetKey(i)

Next i

MsgBox t00

End With

对元素降序排序

SortedList对所有元素进行升序排序。要按降序获取元素,使用GetByIndex方法降序读取/获取元素。

With sl

Dim str, i, t00

For Each str In Array("aa1", "aa2", "aa3","aa39", "aa22", "aa6")

.Add str, .Count

Next str

For i = .Count - 1 To 0 Step -1

t00 = t00 & vbLf & .GetByIndex(i)

Next i

MsgBox t00

End With

对键降序排序

SortedList对所有键进行升序排序。要按降序获取键,使用GetKey降序读取/获取键。

With sl

Dim str, i, t00

For Each str In Array("aa1", "aa2", "aa9","aa8", "aa12", "aa6")

.Add str, .Count

Next str

For i = .Count - 1 To 0 Step -1

t00 = t00 & vbLf & .GetKey(i)

Next i

MsgBox t00

End With

删除元素

通过键删除元素:Remove

Remove方法删除SortedList中的1个元素,括号中的参数是键值。如果键不存在,则不会发生任何事情;VBA不会产生错误。下面的代码删除键为“aa3”的元素:

With sl

Dim str

For Each str In Array("aa1", "aa2", "aa8","aa3", "aa12", "aa6")

.Add str, .Count * 2

Next str

MsgBox .Count

.Remove "aa3"

MsgBox .Count

End With

通过索引号删除元素:RemoveAt

RemoveAt方法删除参数中索引编号位置的元素。如果索引号不存在,VBA将产生一条错误消息。第一个元素的索引号为0。下面的代码删除第5个元素:

With sl

Dim str

For Each str In Array("aa1", "aa2", "aa3","aa4", "aa5", "aa6")

.Add str, .Count * 2

Next str

MsgBox .Count

.RemoveAt 4

MsgBox .Count

End With

基于其内容删除元素

SortedList中没有执行此操作的特殊方法。遍历sortedlist并将元素内容与条件进行比较是解决问题的方法。

可以同时使用Remove方法和RemoveAt方法来删除元素项。每次删除元素时,所有索引号也会更改。因此,删除多个元素项的最佳方法是使用降序遍历。

下面的代码通过索引号删除:

With sl

Dim str, i

For Each str In Array("aa1", "aa2", "aa8","aa3", "aa12", "aa6")

.Add str, .Count * 2

Next str

MsgBox .Count

For i = .Count - 1 To 0 Step -1

If .GetByIndex(i) = 10 Then

.RemoveAt i

End If

Next i

MsgBox .Count

End With

下面的代码通过键来删除:

With sl

Dim str, i

For Each str In Array("aa1", "aa2", "aa8","aa3", "aa12", "aa6")

.Add str, .Count * 2

Next str

MsgBox .Count

For i = .Count - 1 To 0 Step -1

If .GetByIndex(i) = 10 Then

.Remove .GetKey(i)

End If

Next i

MsgBox .Count

End With

删除所有元素:Clear

Clear方法删除SortedList中的所有元素。

With sl

Dim str, i

For Each str In Array("aa1", "aa2", "aa8","aa3", "aa12", "aa6")

.Add str, .Count * 2

Next str

.Clear

MsgBox .Count

End With

复制SortedList:Clone

可以使用Clone方法创建SortedList的副本。副本中的修改不会影响原来的SortedList。由于副本也是一个对象(= SortedList),因此需要使用Set语句将副本赋值给变量。

With sl

Dim str, slCopy

For Each str In Array("aa1", "aa2", "aa8","aa3", "aa12", "aa6")

.Add str, str

Next str

Set slCopy = .Clone

MsgBox slCopy.Count

slCopy.Item("aa2") = "*"

MsgBox .Item("aa2") & vbTab &slCopy.Item("aa2")

End With

注:本文学习整理自snb-vba.eu,建议感兴趣的朋友边阅读边自已输入代码调试来更好地学习这些知识。

0 人点赞