《程序員的第一年》---------- 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.exe

Key = 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.exe

Value = notepad.exe
Value = paint.exe
Value = paint.exe
Value = winword.exe
Value = winword.exe
Value = hypertrm.exe

Key = txt
Key = bmp
Key = dib
Key = rtf
Key = doc
Key = ht

Remove("doc")
Key "doc" is not found.
*/
 




http://www.5ufanli.net


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章