字典、集合和数组的比较 [英] Comparison of Dictionary, Collections and Arrays

查看:47
本文介绍了字典、集合和数组的比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找出字典与集合和数组相比的相对优势和特性.

I am trying to work out the relative benefits and features of dictionaries compared with collections and arrays.

我在这里找到了一篇很棒的文章 但找不到一个简单的表格来比较所有不同的功能.

I found an excellent article here but can't find a simple table that compares all the various features.

有人知道吗?

推荐答案

请参阅下表以对集合和词典进行有用的比较.

Please see the table below for a useful comparison of collections and dictionaries.

(表格总结了这个页面 直到关于早期和晚期绑定"的部分.仅供参考,该页面还提供了有关使用词典的更多详细信息)

(The table summarises this page up to the section on "Early And late binding". FYI the page also has more detailed info about using dictionaries)

总而言之,通常最好使用字典或数组.

In summary it's usually best to use a dictionary or an array.

在考虑使用集合时,如果大小不变或很少变化,则使用数组可能更合适.在这种情况下,数组可能比集合更有效,因为数组可以非常有效地一次填充和检索所有项目(例如,范围到数组和数组返回到范围).

When considering using collections it may be more appropriate to use an array if the size does not change, or changes only rarely. In this case an array is likely to be more efficient than a collection as Arrays are very efficient to populate and retrieve all items at once (eg. range to array and array back to range).

另请注意:

与数组相比,集合在添加和插入项目以及通过其键访问和删除项目方面提供了良好的性能.但是,如果要通过索引访问项目,则性能很差.有关有效执行此操作的信息,请参阅此处还讨论了这些列表对象的内部工作原理.

Compared to Arrays, collections offer good performance for adding and inserting items, and accessing and removing them by their Keys. However, performance is poor if items are to be accessed by index. For information about doing this efficiently see here which also discusses the inner workings of these list objects.

这个 cpearson 页面 有非常有用的代码来处理字典、集合和数组(对它们进行排序,并将它们相互转换!)

This cpearson page has has very useful code for working with dictionaries, collections and arrays (sorting them, and also converting them to be each other!)

来自cpearson页面的一些文字:

Some text from cpearson's page:

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

The Collection object and the Dictionary object are very useful for storing groups of related data. All else being equal, I use a Dictionary object rather than a Collection object because you have access (read, write, change) to the Key property associated with an Item in the Dictionary. In a rather poor object design, the Key of an item in a Collection is write-only. You can assign a Key to an Item when you add the Item to the Collection, but you cannot retrieve the Key associated with an Item nor can you determine (directly) whether a key exists in a Collection. Dictionaries are much friendly and open with their keys. Dictionaries are also considerably faster than Collections.

为什么数组是一个糟糕的选择.数组在重新调整大小和在中间插入项目时要慢得多,因为每个 Redim 将整个内存块复制到更大的位置,如果使用 Preserve,所有值也会被复制.这可能会转化为每个操作的感知缓慢 - 在潜在的应用程序中)

Why can arrays be a bad choice. Arrays are much slower at re-sizing and inserting items in the middle as each Redim copies the entire memory block to a larger location, and if Preserve is used, all values copied over as well. This may translate to perceived slowness for every operation - in a potential application)

VBA 中的集合与字典

Feature                 | COLLECTION | DICTIONARY | Remark
------------------------+------------+------------+--------------------------------
Usually faster          |            |     X      | 
------------------------+------------+------------+--------------------------------
Supported by VB Script  |            |     X      | Collections do not exist in VBS.
------------------------+------------+------------+--------------------------------
                        |            |            | Dicts: Add ref to Miscrosoft 
Native to VBA           |     X      |            | Scripting Library. Usage:
                        |            |            | Dim MyDict As Scripting.Dictionary
                        |            |            | Set MyDict = New Scripting.Dictionary
------------------------+------------+------------+--------------------------------
Can change Keys and     |            |            | Dict properties are writable.
Items                   |            |     X      | For collections, remove the item
                        |            |            | and add a new item.
------------------------+------------+------------+--------------------------------
                        |            |            | A collection enumerates its items:
                        |            |            |  For Each x In MyCollection
                        |            |            |      Debug.Print x
Enumerated              |     X      |     X      |  Next x
                        |            |            | A dict enumerates its keys:
                        |            |            |  For Each x In MyDictionary
                        |            |            |      Debug.Print MyDictionary.Item(x)
                        |            |            |  Next x
------------------------+------------+------------+--------------------------------
                        |            |            | A 1-d array of keys 
Directly output to      |            |            | and items can be returned by 
array                   |            |     X      | dict methods .Keys and .Items.
                        |            |            | (The array is zero-based even 
                        |            |            |  with Option Base 1.)
------------------------+------------+------------+--------------------------------
Retrieve and access     |     X      |     X      |
items                   |            |            |  
------------------------+------------+------------+--------------------------------
Add items               |     X      |     X      |
------------------------+------------+------------+--------------------------------
Implicitly add items    |            |     X      | Dicts can implicitly add items 
                        |            |            | using .Item property.
------------------------+------------+------------+--------------------------------
Remove items            |     X      |     X      |
------------------------+------------+------------+--------------------------------
Remove all items in     |            |            | With collections, each item must
one step                |            |     X      | be removed in turn, or the 
                        |            |            | collection destroyed and recreated.
------------------------+------------+------------+--------------------------------
Count items             |     X      |     X      |
------------------------+------------+------------+--------------------------------
Return item using key   |     X      |     X      |
as lookup value         |            |            |
------------------------+------------+------------+--------------------------------
Return item using       |            |            |
ordinal position        |     X      |   (Slow)   |
as lookup value         |            |            |
------------------------+------------+------------+--------------------------------
Return ordinal          |            |            |
position using item     |     X      |     ??     |
as lookup value         |            |            |
------------------------+------------+------------+--------------------------------
Retrieve and access     |            |     X      | Collection keys only used to
keys                    |            |            | look up data, not retrievable.
------------------------+------------+------------+--------------------------------
Keys optional           |     X      |            | Big + of collections, assuming keys
                        |            |            | are not needed. (Access via index.)
------------------------+------------+------------+--------------------------------
Case sensitivity        |            |     X      |
optional                |            |            |  
------------------------+------------+------------+--------------------------------
                        |            |            | Collection keys must be strings.
Keys can be any type    |            |     X      | Dict keys can have any type
                        |            |            | (except arrays), incl. mixed types.
------------------------+------------+------------+--------------------------------
Keys must be unique     |     X      |     X      |
------------------------+------------+------------+--------------------------------
                        |            |            | * For collections, add code:
                        |            |            |  Public Function _
                        |            |            |     Contains(col As Collection, _
Supports .Exists method |  Remark*   |     X      |     key As Variant) As Boolean
                        |            |            |     On Error Resume Next
                        |            |            |     col(key)
                        |            |            |     Contains = (Err.Number = 0)
------------------------+------------+------------+--------------------------------
Preserve key order when |            |     X      | This is because collection keys 
sorting by item value   |            |            | are write-only, not read. Poor design!

原图,信息更多,排列更清晰:

The original image, which has more information and is more clearly arranged:

这篇关于字典、集合和数组的比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆