• [转载]C# HashTable 遍历与排序


            private void Form1_Load(object sender, EventArgs e)
            {
                Hashtable ht = new Hashtable();
    
                ht.Add("job", "a");
                ht.Add("jobmon", "20");
                
                //单个取值,方法比较特别
                string a = ht["jobmon"].ToString();
                //Console.WriteLine(a);
    
                //第一种方法遍历
                foreach(DictionaryEntry de in ht)
                {
                    Console.WriteLine(de.Key);
                    Console.WriteLine(de.Value);
                }
    
                Console.WriteLine("-------------------------");
                
                //第二种方法遍历
                IDictionaryEnumerator enumerator = ht.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    Console.WriteLine(enumerator.Key);
                    Console.WriteLine(enumerator.Value);
                }
    
                Console.WriteLine("++++++++++++++++++++++++++");
                //hashtable的排序第一种方法,按照键的大小排序
    
                ArrayList al = new ArrayList(ht.Keys);
    
                al.Sort();
                al.Reverse(); //反向排序
    
                foreach (string str in al)
                {
                    Console.WriteLine(str + "  " + ht[str]);
                }
    
                Console.WriteLine("++++++++++++++++++++++++++");
                //hashtable的排序第二种方法,按照值的大小排序
    
                ArrayList alv = new ArrayList(ht.Values);
                alv.Sort();
                
                foreach (string str in alv)
                {
                    IDictionaryEnumerator enumerator2 = sl.GetEnumerator();
    
                    while (enumerator2.MoveNext())
                    {
                        if (str.Equals(enumerator2.Value.ToString()))
                        {
    
                            Console.WriteLine(enumerator2.Key + ":" + enumerator2.Value);        
                        }
                        
                    }
    
                    
                }
    
                Console.WriteLine("++++++++++++++++++++++++++");
                //hashtable的排序第三种方法,用SortedList代替hashtable
    
                SortedList sl = new SortedList();
    
                sl.Add("a", "a1");
                sl.Add("c", "c1");
                sl.Add("b", "b1");
    
                IDictionaryEnumerator enumerator1 = sl.GetEnumerator();
                while (enumerator1.MoveNext())
                {
                    Console.WriteLine(enumerator1.Key);
                    Console.WriteLine(enumerator1.Value);
                }
    
            }


    原文地址:http://blog.csdn.net/zhenniubile/article/details/6079547

  • 相关阅读:
    通过Relect反射方法创建对象,获得对象的方法,输出对象信息
    Spring框架中获取连接池常用的四种方式
    Spring框架的七大模块
    Java线程池的四种创建方式
    递归算法
    将字符串反转的 Java 方法
    [String]split()方法
    [String] intern()方法
    案例>>>用绝对值的方法打印出菱形
    数组的简单理解
  • 原文地址:https://www.cnblogs.com/iack/p/3538253.html
Copyright © 2020-2023  润新知