using System;
using System.Collections; //使用Hashtable时,必须引入这个命名空间
class hashtable
{   
    public static void Main()   
    {
              Hashtable ht=new Hashtable(); //创建一个Hashtable实例
        //key值唯一,value值可以重复.
        ht.Add("E","e");//添加key/键值对  
        ht.Add("A","a");   
        ht.Add("C","c");   
        ht.Add("B","b");   
        string s=(string)ht["A"];   
        if(ht.Contains("E")) //判断哈希表是否包含特定键,其返回值为true或false   
            Console.WriteLine("the E key:exist");   
        ht.Remove("C");//移除一个key/键值对  
        Console.WriteLine(ht["A"]);//此处输出a   
        //ht.Clear();//移除所有元素   
        Console.WriteLine(ht["A"]); //此处将不会有任何输出   

        Console.WriteLine("遍历开始");

         foreach(DictionaryEntry de in ht)   //ht为一个Hashtable实例
        {   
            Console.Write(de.Key+":");//de.Key对应于key/键值对key   
            Console.WriteLine(de.Value);//de.Key对应于key/键值对   
        }

        Console.WriteLine("排序开始");

        ArrayList akeys=new ArrayList(ht.Keys); //别忘了导入System.Collections   
        akeys.Sort(); //按字母顺序进行排序  
         
        foreach(string skey in akeys)   
        {   
            Console.Write(skey+ ":");   
            Console.WriteLine(ht[skey]);//排序后输出  
        }




        Console.Read();
    }   
}

 

相关文章:

  • 2022-03-06
  • 2021-06-15
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-17
猜你喜欢
  • 2021-08-30
  • 2022-12-23
  • 2021-05-29
  • 2021-09-07
  • 2021-09-12
  • 2022-12-23
相关资源
相似解决方案