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

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

问题描述



我发现一篇很好的文章


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.

Does anyone know of one?

解决方案

Please see the attached picture of a useful table of features comparing 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).

Also note:

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.

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!)

Some text from cpearson's page:

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.

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)

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

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