2015-09-09 113 views
5

我正在努力解决字典与集合和数组相比的相对优点和特性。字典,集合和数组比较

我发现了一篇很好的文章here,但是找不到一张比较所有各种功能的简单表格。

有人知道吗?

回答

13

请参阅附加图片的功能比较集合和字典的功能表。

(表总结this page占段的“早期和后期绑定”。FYI的页面也有关于使用字典的更详细信息)

综上所述通常最好使用字典或数组。

当考虑使用集合时,如果大小不变或更改很少,那么使用数组可能更合适。在这种情况下,数组可能比集合更有效,因为数组一次可以高效地填充和检索所有项(例如数组和数组的范围返回范围)。

还要注意:

相较于数组,集合提供添加和插入的项目,并访问和通过它们的键移除它们良好的性能。但是,如果要按索引访问项目,性能很差。有关如何有效执行此操作的信息,请参阅here,其中还讨论了这些列表对象的内部工作原理。

This cpearson page已经具有非常有用的代码与字典,集合和数组工作

一些文本从cpearson的页面(对它们进行排序,并把它们转换成对方!):

收集对象和Dictionary对象对于存储相关数据组的 非常有用。在其他所有条件相同的情况下,我使用的是Dictionary对象而不是Collection对象,因为您有 访问(读取,写入,更改)与字典中的 项关联的Key属性。在相当差的对象设计中,集合中的 项的密钥是只写的。将项目添加到集合时,您可以将项目分配给项目 ,但无法检索与项目关联的 键,也不能确定(直接)集合中是否存在 键。字典非常友好,用钥匙打开 。字典也比 集合快得多。

为什么阵列是一个不好的选择。 由于每个Redim都将整个内存块复制到更大的位置,因此在重新调整大小和插入项目时,数组的速度要慢得多,并且如果使用Preserve,则所有值也都会复制。这可能会转化为每一个操作感知缓慢 - 在潜在的应用程序)

enter image description here

+0

大研究!我有更多的相关链接[here](http:// stackoverflow。com/questions/32447670/hiding-vars-in-strings-vs-using-objects-with-properties/32448252#32448252),也许它会帮助 –

+0

感谢您的反馈并阅读它。我发现你提供的链接很有用,并且把它们中的一些和你的一些文本合并到了我的答案中。 – HarveyFrench

+0

@保护你的评论涵盖了我写的很多东西,但是我喜欢关于表格中的信息的一点是,它让信息一目了然,真正让你更容易地选择是否使用集合。 – HarveyFrench

6
Option Explicit 

Sub CollectionsVSdictionaries() ' Requires ref to "Microsoft Scripting Runtime" Library 
    Dim c As Collection   ' TypeName 1-based indexed 
    Dim d As Dictionary   ' 0-based arrays 
    Set c = New Collection  ' or: "Dim c As New Collection" 
    Set d = New Dictionary  ' or: "Dim d As New Dictionary" 

    c.Add Key:="A", Item:="AA": c.Add Key:="B", Item:="BB": c.Add Key:="C", Item:="CC" 
    d.Add Key:="A", Item:="AA": d.Add Key:="B", Item:="BB": d.Add Key:="C", Item:="CC" 

    Debug.Print TypeName(c) ' -> "Collection" 
    Debug.Print TypeName(d) ' -> "Dictionary" 

    Debug.Print c(3)   ' -> "CC" 
    Debug.Print c("C")   ' -> "CC" 
    'Debug.Print c("CC")  ' --- Invalid --- 

    Debug.Print d("C")   ' -> "CC" 
    Debug.Print d("CC")  ' Adds Key:="CC", Item:="" 
    Debug.Print d.Items(2)  ' -> "CC" 
    Debug.Print d.Keys(2)  ' -> "C" 
    Debug.Print d.Keys()(0)  ' -> "A" - Not well known *************************** 
    Debug.Print d.Items()(0) ' -> "AA" - Not well known *************************** 

    'Collection methods: 
    ' .Add     ' c.Add Item, [Key], [Before], [After] (Key is optional) 
    ' .Count 
    ' .Item(Index)   ' Default property; "c.Item(Index)" same as "c(Index)" 
    ' .Remove(Index) 
    'Dictionary methods: 
    ' .Add     ' d.Add Key, Item (Key is required, and must be unique) 
    ' .CompareMode   ' 1. BinaryCompare  - case-sensitive ("A" < "a") 
    ' .CompareMode   ' 2. DatabaseCompare - MS Access only 
    ' .CompareMode   ' 3. TextCompare  - case-insensitive ("A" = "a") 
    ' .Count 
    ' .Exists(Key)   ' Boolean ********************************************** 
    ' .Item(Key) 
    ' .Items     ' Returns full array: .Items(0)(0) 
    ' .Key(Key) 
    ' .Keys     ' Returns full array: .Keys(0)(0) 
    ' .Remove(Key) 
    ' .RemoveAll    ' ****************************************************** 
End Sub 
+0

谢谢保罗 - 这是一个有用的资源。 – HarveyFrench