. net有很多复杂的数据结构。不幸的是,其中一些非常相似,我不总是确定什么时候使用一个,什么时候使用另一个。我的大多数c#和VB书籍都在一定程度上讨论了它们,但它们从来没有真正深入任何细节。
数组、数组列表、列表、哈希表、字典、排序列表和排序字典之间的区别是什么?
哪些是可枚举的(IList -可以做'foreach'循环)?哪些使用键/值对(IDict)?
内存占用呢?插入速度?检索速度?
还有其他值得一提的数据结构吗?
我还在寻找内存使用和速度的更多细节(大o符号)
. net有很多复杂的数据结构。不幸的是,其中一些非常相似,我不总是确定什么时候使用一个,什么时候使用另一个。我的大多数c#和VB书籍都在一定程度上讨论了它们,但它们从来没有真正深入任何细节。
数组、数组列表、列表、哈希表、字典、排序列表和排序字典之间的区别是什么?
哪些是可枚举的(IList -可以做'foreach'循环)?哪些使用键/值对(IDict)?
内存占用呢?插入速度?检索速度?
还有其他值得一提的数据结构吗?
我还在寻找内存使用和速度的更多细节(大o符号)
当前回答
下面是给你的一些建议:
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.
其他回答
首先,. 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)。
我同情这个问题——我也发现(发现?)这个选择令人困惑,所以我开始科学地看看哪个数据结构是最快的(我用VB做了测试,但我想c#会是一样的,因为这两种语言在CLR级别上做同样的事情)。您可以在这里看到我进行的一些基准测试结果(还有一些关于在哪种情况下使用哪种数据类型最好的讨论)。
我能想到的是:
Array* - represents an old-school memory array - kind of like a alias for a normal type[] array. Can enumerate. Can't grow automatically. I would assume very fast insert and retrival speed. ArrayList - automatically growing array. Adds more overhead. Can enum., probably slower than a normal array but still pretty fast. These are used a lot in .NET List - one of my favs - can be used with generics, so you can have a strongly typed array, e.g. List<string>. Other than that, acts very much like ArrayList Hashtable - plain old hashtable. O(1) to O(n) worst case. Can enumerate the value and keys properties, and do key/val pairs Dictionary - same as above only strongly typed via generics, such as Dictionary<string, string> SortedList - a sorted generic list. Slowed on insertion since it has to figure out where to put things. Can enum., probably the same on retrieval since it doesn't have to resort, but deletion will be slower than a plain old list.
我倾向于一直使用List和Dictionary——一旦开始使用带有泛型的强类型,就很难再回到标准的非泛型了。
还有很多其他的数据结构,比如KeyValuePair,你可以用它做一些有趣的事情,还有SortedDictionary,它也很有用。
它们在智能感知上拼得很好。只需输入System.Collections。或者System.Collections.Generics(首选),你会得到一个可用的列表和简短描述。