zoukankan      html  css  js  c++  java
  • HybridDictionary 类

    建议将该类用于字典中的元素数量未知的情况。它利用了 ListDictionary 处理小集合时性能改善的优点,同时也可灵活地切换到处理较大集合时能力比 ListDictionary 更好的 Hashtable

    如果集合的初始大小大于 ListDictionary 的最佳大小,那么集合立即存储在 Hashtable 中,以避免将元素从 ListDictionary 复制到 Hashtable 产生的系统开销。

    构造函数接受一个布尔值参数,该参数使用户可以指定在比较字符串时集合是否忽略大小写。如果集合区分大小写,那么它就使用键对 Object.GetHashCodeObject.Equals 的实现。如果集合不区分大小写,它就执行简单的不区分大小写的顺序比较,这种比较只服从固定区域性的大小写规则。默认情况下,集合区分大小写。有关固定区域性的更多信息,请参见 System.Globalization.CultureInfo

    键不能是空引用(Visual Basic 中为 Nothing),但值可以

    Visual Basic, C#] C# 语言中的 foreach 语句(在 Visual Basic 中为 for each)需要集合中每个元素的类型。由于 HybridDictionary 的每个元素都是一个键/值对,因此元素类型既不是键的类型,也不是值的类型。而是 DictionaryEntry 类型。例如:
    [C#]
    using System;
    using System.Collections;
    using System.Collections.Specialized;

    public class SamplesHybridDictionary  {

       public static void Main()  {

          // Creates and initializes a new HybridDictionary.
          HybridDictionary myCol = new HybridDictionary();
          myCol.Add( "Braeburn Apples", "1.49" );
          myCol.Add( "Fuji Apples", "1.29" );
          myCol.Add( "Gala Apples", "1.49" );
          myCol.Add( "Golden Delicious Apples", "1.29" );
          myCol.Add( "Granny Smith Apples", "0.89" );
          myCol.Add( "Red Delicious Apples", "0.99" );
          myCol.Add( "Plantain Bananas", "1.49" );
          myCol.Add( "Yellow Bananas", "0.79" );
          myCol.Add( "Strawberries", "3.33" );
          myCol.Add( "Cranberries", "5.98" );
          myCol.Add( "Navel Oranges", "1.29" );
          myCol.Add( "Grapes", "1.99" );
          myCol.Add( "Honeydew Melon", "0.59" );
          myCol.Add( "Seedless Watermelon", "0.49" );
          myCol.Add( "Pineapple", "1.49" );
          myCol.Add( "Nectarine", "1.99" );
          myCol.Add( "Plums", "1.69" );
          myCol.Add( "Peaches", "1.99" );

          // Displays the values in the HybridDictionary in three different ways.
          Console.WriteLine( "Displays the elements using foreach:" );
          PrintKeysAndValues( myCol );
          Console.WriteLine( "Displays the elements using the IDictionaryEnumerator:" );
          PrintKeysAndValues2( myCol );
          Console.WriteLine( "Displays the elements using the Keys, Values, Count, and indexer properties:" );
          PrintKeysAndValues3( myCol );

          // Copies the HybridDictionary to an array with DictionaryEntry elements.
          DictionaryEntry[] myArr = new DictionaryEntry[myCol.Count];
          myCol.CopyTo( myArr, 0 );

          // Displays the values in the array.
          Console.WriteLine( "Displays the elements in the array:" );
          Console.WriteLine( "   KEY                       VALUE" );
          for ( int i = 0; i < myArr.Length; i++ )
             Console.WriteLine( "   {0,-25} {1}", myArr[i].Key, myArr[i].Value );
          Console.WriteLine();

          // Searches for a key.
          if ( myCol.Contains( "Kiwis" ) )
             Console.WriteLine( "The collection contains the key \"Kiwis\"." );
          else
             Console.WriteLine( "The collection does not contain the key \"Kiwis\"." );
          Console.WriteLine();

          // Deletes a key.
          myCol.Remove( "Plums" );
          Console.WriteLine( "The collection contains the following elements after removing \"Plums\":" );
          PrintKeysAndValues( myCol );

          // Clears the entire collection.
          myCol.Clear();
          Console.WriteLine( "The collection contains the following elements after it is cleared:" );
          PrintKeysAndValues( myCol );

       }

       public static void PrintKeysAndValues( IDictionary myCol )  {
          Console.WriteLine( "   KEY                       VALUE" );
          foreach ( DictionaryEntry de in myCol )
             Console.WriteLine( "   {0,-25} {1}", de.Key, de.Value );
          Console.WriteLine();
       }

       public static void PrintKeysAndValues2( IDictionary myCol )  {
          IDictionaryEnumerator myEnumerator = myCol.GetEnumerator();
          Console.WriteLine( "   KEY                       VALUE" );
          while ( myEnumerator.MoveNext() )
             Console.WriteLine( "   {0,-25} {1}", myEnumerator.Key, myEnumerator.Value );
          Console.WriteLine();
       }

       public static void PrintKeysAndValues3( HybridDictionary myCol )  {
          String[] myKeys = new String[myCol.Count];
          myCol.Keys.CopyTo( myKeys, 0 );

          Console.WriteLine( "   INDEX KEY                       VALUE" );
          for ( int i = 0; i < myCol.Count; i++ )
             Console.WriteLine( "   {0,-5} {1,-25} {2}", i, myKeys[i], myCol[myKeys[i]] );
          Console.WriteLine();
       }

    }

    /*
    This code produces the following output.

    Displays the elements using foreach:
       KEY                       VALUE
       Seedless Watermelon       0.49
       Nectarine                 1.99
       Cranberries               5.98
       Plantain Bananas          1.49
       Honeydew Melon            0.59
       Pineapple                 1.49
       Strawberries              3.33
       Grapes                    1.99
       Braeburn Apples           1.49
       Peaches                   1.99
       Red Delicious Apples      0.99
       Golden Delicious Apples   1.29
       Yellow Bananas            0.79
       Granny Smith Apples       0.89
       Gala Apples               1.49
       Plums                     1.69
       Navel Oranges             1.29
       Fuji Apples               1.29

    Displays the elements using the IDictionaryEnumerator:
       KEY                       VALUE
       Seedless Watermelon       0.49
       Nectarine                 1.99
       Cranberries               5.98
       Plantain Bananas          1.49
       Honeydew Melon            0.59
       Pineapple                 1.49
       Strawberries              3.33
       Grapes                    1.99
       Braeburn Apples           1.49
       Peaches                   1.99
       Red Delicious Apples      0.99
       Golden Delicious Apples   1.29
       Yellow Bananas            0.79
       Granny Smith Apples       0.89
       Gala Apples               1.49
       Plums                     1.69
       Navel Oranges             1.29
       Fuji Apples               1.29

    Displays the elements using the Keys, Values, Count, and indexer properties:
       INDEX KEY                       VALUE
       0     Seedless Watermelon       0.49
       1     Nectarine                 1.99
       2     Cranberries               5.98
       3     Plantain Bananas          1.49
       4     Honeydew Melon            0.59
       5     Pineapple                 1.49
       6     Strawberries              3.33
       7     Grapes                    1.99
       8     Braeburn Apples           1.49
       9     Peaches                   1.99
       10    Red Delicious Apples      0.99
       11    Golden Delicious Apples   1.29
       12    Yellow Bananas            0.79
       13    Granny Smith Apples       0.89
       14    Gala Apples               1.49
       15    Plums                     1.69
       16    Navel Oranges             1.29
       17    Fuji Apples               1.29

    Displays the elements in the array:
       KEY                       VALUE
       Seedless Watermelon       0.49
       Nectarine                 1.99
       Cranberries               5.98
       Plantain Bananas          1.49
       Honeydew Melon            0.59
       Pineapple                 1.49
       Strawberries              3.33
       Grapes                    1.99
       Braeburn Apples           1.49
       Peaches                   1.99
       Red Delicious Apples      0.99
       Golden Delicious Apples   1.29
       Yellow Bananas            0.79
       Granny Smith Apples       0.89
       Gala Apples               1.49
       Plums                     1.69
       Navel Oranges             1.29
       Fuji Apples               1.29

    The collection does not contain the key "Kiwis".

    The collection contains the following elements after removing "Plums":
       KEY                       VALUE
       Seedless Watermelon       0.49
       Nectarine                 1.99
       Cranberries               5.98
       Plantain Bananas          1.49
       Honeydew Melon            0.59
       Pineapple                 1.49
       Strawberries              3.33
       Grapes                    1.99
       Braeburn Apples           1.49
       Peaches                   1.99
       Red Delicious Apples      0.99
       Golden Delicious Apples   1.29
       Yellow Bananas            0.79
       Granny Smith Apples       0.89
       Gala Apples               1.49
       Navel Oranges             1.29
       Fuji Apples               1.29

    The collection contains the following elements after it is cleared:
       KEY                       VALUE

    */

  • 相关阅读:
    牛客 小乐乐和25
    codeforces 1303 D 二进制瞎搞
    codeforces 1307 D 最短路bz+贪心
    codeforces 1316 C math
    codeforces 1328E LCA
    codeforces 1335 E2 思维
    codeforces 1335 E1 思维
    codeforces 1342 D 贪心+后缀和
    codeforces 1348D (思维+贪心)
    codeforces 1362 E 进制的性质
  • 原文地址:https://www.cnblogs.com/wzyexf/p/358166.html
Copyright © 2011-2022 走看看