跳轉至內容

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
華夏公益教科書