55 lines
1.9 KiB
Markdown
55 lines
1.9 KiB
Markdown
![]() |
#unity/日常积累
|
|||
|
|
|||
|
### **一.介绍:**
|
|||
|
|
|||
|
SortedDictionary<key,value>,从字面意思不难看出来是有序字典
|
|||
|
|
|||
|
SortedList<key,value>,有序列表
|
|||
|
|
|||
|
### **二.使用:**
|
|||
|
|
|||
|
``` cs
|
|||
|
//SortedDictionary
|
|||
|
SortedDictionary<int, string> sortDic = new SortedDictionary<int, string>();
|
|||
|
sortDic[2] = "2";
|
|||
|
sortDic[1] = "1";
|
|||
|
sortDic[9] = "9";
|
|||
|
sortDic[4] = "4";
|
|||
|
Console.WriteLine("SortedDictionary:");
|
|||
|
foreach (var item in sortDic)
|
|||
|
{
|
|||
|
Console.WriteLine(item.Key);
|
|||
|
}
|
|||
|
|
|||
|
//Dictionary
|
|||
|
Dictionary<int, string> Dic = new Dictionary<int, string>();
|
|||
|
Dic[2] = "2";
|
|||
|
Dic[1] = "1";
|
|||
|
Dic[9] = "9";
|
|||
|
Dic[4] = "4";
|
|||
|
Console.WriteLine("Dictionary:");
|
|||
|
foreach (var item in Dic)
|
|||
|
{
|
|||
|
Console.WriteLine(item.Key);
|
|||
|
}
|
|||
|
|
|||
|
//SortedList
|
|||
|
SortedList<int, string> sortList = new SortedList<int, string>();
|
|||
|
sortList[2] = "2";
|
|||
|
sortList[1] = "1";
|
|||
|
sortList[9] = "9";
|
|||
|
sortList[4] = "4";
|
|||
|
Console.WriteLine("sortList:");
|
|||
|
foreach (var item in sortList)
|
|||
|
{
|
|||
|
Console.WriteLine(item.Key);
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
控制台输出=>可以看出普通Dictionary,没有默认排序,SortedDictionary和SortedList则默认实现字典排序
|
|||
|
|
|||
|
![[Pasted image 20240311133404.png]]
|
|||
|
|
|||
|
三.SortedDictionary<Key,Value>、SortedList<Key,Value>的差异:
|
|||
|
SortedDictionary<Key,Value>使用的内存比SortedList<Key,Value>多
|
|||
|
2.SortedDictionary<Key,Value>删除比较快,插入有序数据时SortedList<Key,Value>较快,而插入无序数据时SortedList<Key,Value>非常慢,查询SortedList<Key,Value>比SortedDictionary<Key,Value>较快。(亲测,环境net4.0),SortedList移除特别慢,耗时约为SortedDictionary的80倍。
|