跳转至内容

Visual Basic/集合

来自 Wikibooks,面向开放世界的开放书籍

集合类提供类数组容器,在某些方面比数组更灵活,而在其他方面则不太灵活。比集合更灵活的类是字典类。

创建新集合

  Dim Cats As Collection
  Set Cats = New Collection

在一行中调整和创建新集合的尺寸

  Dim Cats As New Collection

添加项目

   Cats.Add "Item"
   Cats.Add "Item", "Key"
  ...

以只读方式按索引访问项目

  Cats (3) 'The third member of the collection
  Cats.Item(3) 'An alternative
  Cats.Item("Key 3") 'Works if an item has a key associated

按索引覆盖项目

   NewValue = MyCollection(i) + 1
   MyCollection.Add NewValue, Before:=i
   MyCollection.Remove Index:=i + 1

移除项目

  Collection.Remove Index
  Collection.Remove HashKey

集合大小

  Cats.Count

以只读方式遍历集合

  For Each Cat In Cats
     Rem Do something on Cat
  Next

以读写方式遍历集合

  'Fill the collection
  Set MyCollection = New Collection
  For i = 1 To 10
    MyCollection.Add i
  Next
  'Increment each item by 1
  For i = 1 To MyCollection.Count
    NewValue = MyCollection(i) + 1
    MyCollection.Add NewValue, Before:=i
    MyCollection.Remove Index:=i + 1
  Next

测试集合是否为空

  If Cats.Count=0 Then
    '...
  End If

测试集合中是否存在某个元素

  MatchFound = False
  For Each Member In MyCollection
    If Member = "SoughtValue" Then
      MatchFound = True
      Exit For
    End If
  Next

将一个集合附加到另一个集合

  For Each Member In AppendedCollection
    ExtendedCollection.Add Member
  Next

将集合转换为数组

  Dim MyArray
  ReDim MyArray(MyCollection.Count - 1)
  Index = 0
  For Each Member In MyCollection
    MyArray(Index) = Member
    Index = Index + 1
  Next

将集合用作队列

  Set Queue = New Collection
  For i = 1 To 10
    Queue.Add i
  Next
  For i = 1 To 5
    Queue.Remove 1 'Remove from the queue
  Next

将集合用作栈

  Set Stack = New Collection
  For i = 1 To 10
    Stack.Add i
  Next
  For i = 1 To 5
    Stack.Remove Stack.Count 'Remove from the stack
  Next

将集合用作集合

  ' Using the key of a collection item will do the trick.
  ' The key has to be a string, hence "Str(i)" below.
  ' Note that Str(1) is " 1", with a leading space.
  Set NumberSet = New Collection
  For i = 1 To 10
    NumberSet.Add i, Str(i) 'Value, Key
  Next
  For i = 5 To 15   
    'Add while catching errors resulting from existence of the key
    On Error Resume Next
    NumberSet.Add i, Str(i) 'Value, Key
    On Error GoTo 0
    'End If
  Next
  For i = 14 To 16
    'Remove
    On Error Resume Next 'Catch error if element not present
    NumberSet.Remove Str(i)
    On Error GoTo 0
  Next
  'Membership test
  On Error Resume Next
  NumberSet.Item(Str(50))
  IsMember=Err.Number=0
  On Error GoTo 0
  '
  Set NumberSet2 = New Collection
  For i = 10 To 25
    NumberSet2.Add i, Str(i)
  Next
  'Union
  Set SetUnion = New Collection
  For Each Item In NumberSet
    SetUnion.Add Item, Str(Item) 'Value, Key
  Next
  For Each Item In NumberSet2
    On Error Resume Next
    SetUnion.Add Item, Str(Item) 'Value, Key
    On Error GoTo 0
  Next
  'Intersection
  Set SetIntersection = New Collection
  For Each Item In NumberSet
    On Error Resume Next
    Dummy = NumberSet2(Str(Item))
    If Err.Number = 0 Then
      SetIntersection.Add Item, Str(Item) 'Value, Key
    End If
    On Error GoTo 0
  Next
  'Set difference
  Set SetDifference = New Collection
  For Each Item In NumberSet
    On Error Resume Next
    Dummy = NumberSet2(Str(Item))
    If Err.Number <> 0 Then
      SetDifference.Add Item, Str(Item) 'Value, Key
    End If
    On Error GoTo 0
  Next
华夏公益教科书