且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

字典,集合和数组比较

更新时间:2023-11-29 19:03:58

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

Please see the attached picture of a useful table of features comparing collections and dictionaries.

(表汇总此页的高达一节早期和后期绑定。FYI的页面也有关于使用字典的更详细信息)

(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页面有了非常有用的code与字典工作,集合和数组(对它们进行排序,也将它们转换为对方!)

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:

收集对象和Dictionary对象是存储相关数据组非常有用的。在其他条件相同,我使用Dictionary对象,而不是一个集合对象,因为你可以访问(读取,写入,更改),以与在字典的项目相关的关键属性。在一个比较差的对象的设计,集合中的一个项目的重点是只写。当您添加项目到收藏您可以将关键的项目,但你不能检索与该产品相关的关键也不能确定(直接)是否存在密钥的集合。字典是多少,并能与他们的钥匙打开。字典也比集合快很多。

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)

字典,集合和数组比较