zoukankan      html  css  js  c++  java
  • C# hashTable的遍历【2种方法】与排序【3种方法】

    1. private void Form1_Load(object sender, EventArgs e)  
    2. {  
    3.     Hashtable ht = new Hashtable();  
    4.   
    5.     ht.Add("job""a");  
    6.     ht.Add("jobmon""20");  
    7.       
    8.     //单个取值,方法比较特别  
    9.     string a = ht["jobmon"].ToString();  
    10.     //Console.WriteLine(a);  
    11.   
    12.     //第一种方法遍历  
    13.     foreach(DictionaryEntry de in ht)  
    14.     {  
    15.         Console.WriteLine(de.Key);  
    16.         Console.WriteLine(de.Value);  
    17.     }  
    18.   
    19.     Console.WriteLine("-------------------------");  
    20.       
    21.     //第二种方法遍历  
    22.     IDictionaryEnumerator enumerator = ht.GetEnumerator();  
    23.     while (enumerator.MoveNext())  
    24.     {  
    25.         Console.WriteLine(enumerator.Key);  
    26.         Console.WriteLine(enumerator.Value);  
    27.     }  
    28.   
    29.     Console.WriteLine("++++++++++++++++++++++++++");  
    30.     //hashtable的排序第一种方法,按照键的大小排序  
    31.   
    32.     ArrayList al = new ArrayList(ht.Keys);  
    33.   
    34.     al.Sort();  
    35.     al.Reverse(); //反向排序  
    36.   
    37.     foreach (string str in al)  
    38.     {  
    39.         Console.WriteLine(str + "  " + ht[str]);  
    40.     }  
    41.   
    42.     Console.WriteLine("++++++++++++++++++++++++++");  
    43.     //hashtable的排序第二种方法,按照值的大小排序  
    44.   
    45.     ArrayList alv = new ArrayList(ht.Values);  
    46.     alv.Sort();  
    47.       
    48.     foreach (string str in alv)  
    49.     {  
    50.         IDictionaryEnumerator enumerator2 = sl.GetEnumerator();  
    51.   
    52.         while (enumerator2.MoveNext())  
    53.         {  
    54.             if (str.Equals(enumerator2.Value.ToString()))  
    55.             {  
    56.   
    57.                 Console.WriteLine(enumerator2.Key + ":" + enumerator2.Value);          
    58.             }  
    59.               
    60.         }  
    61.   
    62.           
    63.     }  
    64.   
    65.     Console.WriteLine("++++++++++++++++++++++++++");  
    66.     //hashtable的排序第三种方法,用SortedList代替hashtable  
    67.   
    68.     SortedList sl = new SortedList();  
    69.   
    70.     sl.Add("a""a1");  
    71.     sl.Add("c""c1");  
    72.     sl.Add("b""b1");  
    73.   
    74.     IDictionaryEnumerator enumerator1 = sl.GetEnumerator();  
    75.     while (enumerator1.MoveNext())  
    76.     {  
    77.         Console.WriteLine(enumerator1.Key);  
    78.         Console.WriteLine(enumerator1.Value);  
    79.     }  
    80.   
    81. }  
    82.  
  • 相关阅读:
    ASP.NET AJAX Progress Bar Control(转)
    asp.net 页面右下角弹出类似QQ或MSN的消息提示
    用31个免费在线工具来测试你网站各项性能
    HtmlTextWriter学习<转>
    .NET企业级应用架构设计系列之应用服务器
    .NET企业级应用架构设计系列之开场白
    Asp.Net应用程序中长时间装载页面时显示进度条
    用JavaScript操作数据库
    UML类图关系全面剖析
    Microsoft® Visual SourceSafe® 6.0 标准版 简介下载
  • 原文地址:https://www.cnblogs.com/lujiangping/p/10630036.html
Copyright © 2011-2022 走看看