[转载]C# HashTable 遍历与排序
2014-02-05 13:32
411 查看
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
相关文章推荐
- C#中hashtable的赋值、取值、遍历、排序操作
- c# hashTable的遍历【2种方法】与排序【3种方法】
- C#.NET 对HashTable数组进行按值排序
- C#数组和集合专题5(Hashtable排序和遍历)
- c#遍历HashTable
- [转]c#遍历HashTable的方法
- C#中HashTable的遍历
- C# WF 遍历获取容器内的子元素并进行重新排序
- C# 遍历Hashtable
- C#常见的几种集合 ArrayList,Hashtable,List<T>,Dictionary<K,V> 遍历方法对比
- [转载]:合并两个已排序好的int数组,并排序返回c#实现
- C# 遍历Hashtable
- [转]C# ListView 单击标题实现排序(在转载的基础上有所完善)
- asp.net(C#)遍历文件夹下所有文件,绑定到GRIDVIEW并排序
- C#控制台基础 遍历输出hashtable中所有的键和值
- C# 遍历Hashtable
- C# 遍历Hashtable
- c#调用api(FindFirstFile,FindNextFile)高效遍历目录文件【转载】
- C#遍历HashTable
- 如何在C#中遍历hashtable?