. net有很多复杂的数据结构。不幸的是,其中一些非常相似,我不总是确定什么时候使用一个,什么时候使用另一个。我的大多数c#和VB书籍都在一定程度上讨论了它们,但它们从来没有真正深入任何细节。

数组、数组列表、列表、哈希表、字典、排序列表和排序字典之间的区别是什么?

哪些是可枚举的(IList -可以做'foreach'循环)?哪些使用键/值对(IDict)?

内存占用呢?插入速度?检索速度?

还有其他值得一提的数据结构吗?

我还在寻找内存使用和速度的更多细节(大o符号)


当前回答

它们在智能感知上拼得很好。只需输入System.Collections。或者System.Collections.Generics(首选),你会得到一个可用的列表和简短描述。

其他回答

泛型集合将比非泛型集合执行得更好,特别是在遍历许多项时。这是因为装箱和开箱不再发生。

首先,. net中的所有集合都实现了IEnumerable。

其次,很多集合是重复的,因为泛型是在框架的2.0版本中添加的。

因此,尽管泛型集合可能会添加功能,但在大多数情况下:

List是ArrayList的通用实现。 Dictionary<T,K>是Hashtable的泛型实现

数组是一个固定大小的集合,您可以更改存储在给定索引中的值。

SortedDictionary是一个<T,K>的字典,它基于键进行排序。 SortedList是一个<T,K>的字典,它基于所需的IComparer进行排序。

因此,字典实现(那些支持KeyValuePairs)是:

哈希表 词典<T,F> 排序列表<T,F> SortedDictionary<T,K>

. net 3.5中添加的另一个集合是Hashset。它是一个支持集合操作的集合。

而且,LinkedList是一个标准的链表实现(List是一个用于更快检索的数组列表)。

哈希表/字典是O(1)性能,这意味着性能不是大小的函数。知道这一点很重要。

编辑:在实践中,哈希表/字典<>查找的平均时间复杂度是O(1)。

下面是给你的一些建议:

You can use foreach on types that implement IEnumerable. IList is essentially an IEnumberable with Count and Item (accessing items using a zero-based index) properties. IDictionary on the other hand means you can access items by any-hashable index. Array, ArrayList and List all implement IList. Dictionary, SortedDictionary, and Hashtable implement IDictionary. If you are using .NET 2.0 or higher, it is recommended that you use generic counterparts of mentioned types. For time and space complexity of various operations on these types, you should consult their documentation. .NET data structures are in System.Collections namespace. There are type libraries such as PowerCollections which offer additional data structures. To get a thorough understanding of data structures, consult resources such as CLRS.

它们在智能感知上拼得很好。只需输入System.Collections。或者System.Collections.Generics(首选),你会得到一个可用的列表和简短描述。