类型实现《程序员的第一年》--------------C#中System.Collections.Generic.SortedDictionary 的使用...
生活随笔
收集整理的這篇文章主要介紹了
类型实现《程序员的第一年》--------------C#中System.Collections.Generic.SortedDictionary 的使用...
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在改章節中,我們主要介紹類型實現的內容,自我感覺有個不錯的建議和大家分享下
SortedDictionary<TKey,TValue> 類型參數 TKey 字典中的鍵的類型。 TValue 字典中的值的類型。 SortedDictionary 泛型類是索檢算運復雜度為 O(log n) 的二叉搜索樹,其中 n 是字典中的元素數。就這一點而言,它與 SortedList 泛型類相似。這兩個類擁有相似的對象模型,并且都擁有 O(log n) 的索檢算運復雜度。這兩個類的別區在于內存的應用以及入插和移除元素的速度: SortedList 應用的內存比 SortedDictionary 少。 SortedDictionary 可對未排序的據數執行更快的入插和移除作操:它的時間復雜度為 O(log n),而 SortedList 為 O(n)。 如果應用排序據數一次性填充列表,則 SortedList 比 SortedDictionary 快。 個每鍵/值對都可以作為 KeyValuePair 構結行進索檢,或作為 DictionaryEntry 通過非泛型 IDictionary 接口行進檢索。 只要鍵用作 SortedDictionary 中的鍵,它們就必須是不可變的。SortedDictionary 中的個每鍵必須是一唯的。鍵不能為 空引用(在 Visual Basic 中為 Nothing),但是如果值類型 TValue 為引用類型,該值則可認為空。 SortedDictionary 須要比擬器實現來執行鍵比擬。可以應用一個接受 comparer 參數的構造函數來指定 IComparer 泛型接口的實現;如果不指定實現,則應用認默的泛型比擬器 Comparer.Default。如果類型 TKey 實現 System.IComparable 泛型接口,則認默比擬器應用該實現。 C# 言語的 foreach 語句,須要集合中個每元素的類型。由于 SortedDictionary 的個每元素都是一個鍵/值對,因此元素類型既不是鍵的類型,也不是值的類型。而是 KeyValuePair 類型。上面的碼代演示 C# 法語 using System; using System.Collections.Generic;public class Example {public static void Main(){// Create a new dictionary of strings, with string keys.//Dictionary<string, string> openWith =new Dictionary<string, string>();// Add some elements to the dictionary. There are no// duplicate keys, but some of the values are duplicates.openWith.Add("txt", "notepad.exe");openWith.Add("bmp", "paint.exe");openWith.Add("dib", "paint.exe");openWith.Add("rtf", "wordpad.exe");// The Add method throws an exception if the new key is// already in the dictionary.try{openWith.Add("txt", "winword.exe");}catch (ArgumentException){Console.WriteLine("An element with Key = \"txt\" already exists.");}// The Item property is another name for the indexer, so you// can omit its name when accessing elements.Console.WriteLine("For key = \"rtf\", value = {0}.",openWith["rtf"]);// The indexer can be used to change the value associated// with a key.openWith["rtf"] = "winword.exe";Console.WriteLine("For key = \"rtf\", value = {0}.",openWith["rtf"]);// If a key does not exist, setting the indexer for that key// adds a new key/value pair.openWith["doc"] = "winword.exe";// The indexer throws an exception if the requested key is// not in the dictionary.try{Console.WriteLine("For key = \"tif\", value = {0}.",openWith["tif"]);}catch (KeyNotFoundException){Console.WriteLine("Key = \"tif\" is not found.");}// When a program often has to try keys that turn out not to// be in the dictionary, TryGetValue can be a more efficient// way to retrieve values.string value = "";if (openWith.TryGetValue("tif", out value)){Console.WriteLine("For key = \"tif\", value = {0}.", value);}else{Console.WriteLine("Key = \"tif\" is not found.");}// ContainsKey can be used to test keys before inserting// them.if (!openWith.ContainsKey("ht")){openWith.Add("ht", "hypertrm.exe");Console.WriteLine("Value added for key = \"ht\": {0}",openWith["ht"]);}// When you use foreach to enumerate dictionary elements,// the elements are retrieved as KeyValuePair objects.Console.WriteLine();foreach( KeyValuePair<string, string> kvp in openWith ){Console.WriteLine("Key = {0}, Value = {1}",kvp.Key, kvp.Value);}// To get the values alone, use the Values property.Dictionary<string, string>.ValueCollection valueColl =openWith.Values;// The elements of the ValueCollection are strongly typed// with the type that was specified for dictionary values.Console.WriteLine();foreach( string s in valueColl ){Console.WriteLine("Value = {0}", s);}// To get the keys alone, use the Keys property.Dictionary<string, string>.KeyCollection keyColl =openWith.Keys;// The elements of the KeyCollection are strongly typed// with the type that was specified for dictionary keys.Console.WriteLine();foreach( string s in keyColl ){Console.WriteLine("Key = {0}", s);}// Use the Remove method to remove a key/value pair.Console.WriteLine("\nRemove(\"doc\")");openWith.Remove("doc");if (!openWith.ContainsKey("doc")){Console.WriteLine("Key \"doc\" is not found.");}} }* This code example produces the following output:An element with Key = "txt" already exists. For key = "rtf", value = wordpad.exe. For key = "rtf", value = winword.exe. Key = "tif" is not found. Key = "tif" is not found. Value added for key = "ht": hypertrm.exeKey = txt, Value = notepad.exe Key = bmp, Value = paint.exe Key = dib, Value = paint.exe Key = rtf, Value = winword.exe Key = doc, Value = winword.exe Key = ht, Value = hypertrm.exeValue = notepad.exe Value = paint.exe Value = paint.exe Value = winword.exe Value = winword.exe Value = hypertrm.exeKey = txt Key = bmp Key = dib Key = rtf Key = doc Key = htRemove("doc") Key "doc" is not found. */ 每日一道理只有啟程,才會到達理想和目的地,只有拼搏,才會獲得輝煌的成功,只有播種,才會有收獲。只有追求,才會品味堂堂正正的人。
????http://www.5ufanli.net
文章結束給大家分享下程序員的一些笑話語錄: 程序員的愿望
有一天一個程序員見到了上帝.上帝: 小伙子,我可以滿足你一個愿望.程序員: 我希望中國國家隊能再次打進世界杯.
上帝: 這個啊!這個不好辦啊,你還說下一個吧!
程序員: 那好!我的下一個愿望是每天都能休息6個小時以上.
上帝: 還是讓中國國家打進世界杯.
轉載于:https://www.cnblogs.com/jiangu66/archive/2013/04/24/3040992.html
總結
以上是生活随笔為你收集整理的类型实现《程序员的第一年》--------------C#中System.Collections.Generic.SortedDictionary 的使用...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [书目20140322]如何管理软件企业
- 下一篇: 军费保障幼师是部队职工吗