1、添加引用 using System.Collections;
2、创建并添加数据
1 Hashtable hs = new Hashtable); 2 hs.Add"Name1", "lwj"); 3 hs.Add"Name2", "wyp"); 4 hs.Add"Name3", "zwl"); 5 hs.Add"Name4", "zyc"); 6 hs.Add"Name8", "wyw"); 7 hs.Add"Name5", "wyw");
3、遍历哈希表中的 值Value
1 Console.WriteLine"---------遍历哈希表中的值----------"); 2 ////返回循环访问 System.Collections.Hashtable 的 System.Collections.IDictionaryEnumerator。 3 IDictionaryEnumerator emu = hs.GetEnumerator); //// 遍历哈希表所有的键,读出相应的值 4 while emu.MoveNext)) //如果枚举数成功地推进到下一个元素,则为 true;如果枚举数越过集合的结尾,则为 false。 5 { 6 string str = emu.Value.ToString); 7 Console.WriteLinestr); 8 } |
4、遍历哈希表
1 Console.WriteLine"-----------遍历哈希表-------------"); 2 foreach DictionaryEntry de in hs) 3 { 4 Console.WriteLine"key = {0}; Value = {1}", de.Key, de.Value); 5 }
5、对HashTable排序之后输出,按Key排序
1 Console.WriteLine"-------哈希表按Key排序之后--------"); 2 ////哈希表排序, 按Key排序 3 ArrayList alist = new ArrayLisths.Keys); 4 alist.Sort); 5 foreach string obj in alist) // //遍历alist 6 { 7 Console.WriteLine"{0, -15} {1, -15}", obj, hs[obj]); //{0, -15} PadLeft 8 }
6、转换成List输出
1 Console.WriteLine"------------使用List之后-----------"); 2 List<string> list = new List<string>); 3 foreach DictionaryEntry de in hs) 4 { 5 list.Addde.Value.ToString)); 6 } 7 foreach string obj in list) 8 { 9 Console.WriteLineobj);//i就是下标 10 }
7、转换成Dictionary<object, object>之后进行遍历输出
1 Console.WriteLine"---------使用Dictionary之后--------"); 2 Dictionary<object, object> myDic = new Dictionary<object, object>); 3 foreach DictionaryEntry de in hs) //循环遍历HashTable 将其添加至 myDic 4 { 5 myDic.Addde.Key, de.Value); // 这俩都是 object型的 6 } 7 //循环遍历输出 myDic 8 foreach object obj in myDic) 9 { 10 Console.WriteLineobj.ToString)); //[Name,wyw] 输出是这样的格式 11 } 12 //采用另一种输出 13 Console.WriteLine"--------Dictionary键值对输---------"); 14 foreach KeyValuePair<object, object> kvp in myDic) 15 { 16 Console.WriteLine"Key={0}, Value={1}", kvp.Key, kvp.Value); //获取键值对后,自定义输出 17 }
8、克隆HashTable 到另一HashTable并 遍历输出:
//此处写 添加 删除一些数据,以便验证
1 Console.WriteLine"------添加移除之后,遍历哈希表-----"); 2 hs["Name0"] = "The First"; 3 hs.Remove"Name8"); 4 hs.Add"Name6", "add6"); 5 foreach DictionaryEntry de in hs) 6 { 7 Console.WriteLine"Key: {0, 15} Value{1, 15}", de.Key, de.Value); 8 } 9 10 Console.WriteLine"--------克隆哈希表到hs2之后--------"); 11 Hashtable hs2 = Hashtable)hs.Clone); //public virtual object Clone); 创建 System.Collections.Hashtable 的浅表副本。 12 foreach DictionaryEntry de in hs2) 13 { 14 Console.WriteLine"Key: {0, 15} Value{1, 15}", de.Key, de.Value); 15 } 16 17 Console.WriteLine"----------哈希表清空之后-----------"); 18 hs.Clear); 19 foreach DictionaryEntry de in hs) //哈希表存在,但里面没数据,因此下列不执行 20 { 21 Console.WriteLine"Key: {0, 15} Value{1, 15}", de.Key, de.Value); 22 }
全部源代码:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; namespace HashTable { class Program { static void Mainstring[] args) { Hashtable hs = new Hashtable); hs.Add"Name1", "lwj"); hs.Add"Name2", "wyp"); hs.Add"Name3", "zwl"); hs.Add"Name4", "zyc"); hs.Add"Name8", "wyw"); hs.Add"Name5", "wyw"); Console.WriteLine"-----------遍历哈希表-------------"); foreach DictionaryEntry de in hs) { Console.WriteLine"key = {0}; Value = {1}", de.Key, de.Value); } Console.WriteLine"---------遍历哈希表中的值----------"); ////返回循环访问 System.Collections.Hashtable 的 System.Collections.IDictionaryEnumerator。 IDictionaryEnumerator emu = hs.GetEnumerator); //// 遍历哈希表所有的键,读出相应的值 while emu.MoveNext)) //如果枚举数成功地推进到下一个元素,则为 true;如果枚举数越过集合的结尾,则为 false。 { string str = emu.Value.ToString); Console.WriteLinestr); } Console.WriteLine"-------哈希表按Key排序之后--------"); ////哈希表排序, 按Key排序 ArrayList alist = new ArrayLisths.Keys); alist.Sort); foreach string obj in alist) // //遍历alist { Console.WriteLine"{0, -15} {1, -15}", obj, hs[obj]); //{0, -15} PadLeft } Console.WriteLine"------------使用List之后-----------"); List<string> list = new List<string>); foreach DictionaryEntry de in hs) { list.Addde.Value.ToString)); } foreach string obj in list) { Console.WriteLineobj);//i就是下标 } Console.WriteLine"---------使用Dictionary之后--------"); Dictionary<object, object> myDic = new Dictionary<object, object>); foreach DictionaryEntry de in hs) //循环遍历HashTable 将其添加至 myDic { myDic.Addde.Key, de.Value); // 这俩都是 object型的 } //循环遍历输出 myDic foreach object obj in myDic) { Console.WriteLineobj.ToString)); //[Name,wyw] 输出是这样的格式 } //采用另一种输出 Console.WriteLine"--------Dictionary键值对输---------"); foreach KeyValuePair<object, object> kvp in myDic) { Console.WriteLine"Key={0}, Value={1}", kvp.Key, kvp.Value); //获取键值对后,自定义输出 } Console.WriteLine"------添加移除之后,遍历哈希表-----"); hs["Name0"] = "The First"; hs.Remove"Name8"); hs.Add"Name6", "add6"); foreach DictionaryEntry de in hs) { Console.WriteLine"Key: {0, 15} Value{1, 15}", de.Key, de.Value); } Console.WriteLine"--------克隆哈希表到hs2之后--------"); Hashtable hs2 = Hashtable)hs.Clone); //public virtual object Clone); 创建 System.Collections.Hashtable 的浅表副本。 foreach DictionaryEntry de in hs2) { Console.WriteLine"Key: {0, 15} Value{1, 15}", de.Key, de.Value); } Console.WriteLine"----------哈希表清空之后-----------"); hs.Clear); foreach DictionaryEntry de in hs) //哈希表存在,但里面没数据,因此下列不执行 { Console.WriteLine"Key: {0, 15} Value{1, 15}", de.Key, de.Value); } Console.ReadKey); } } }
笔者在接触到了HashTable之后,上网搜索了一下具体用法,然后笔者就试着去联系以前 接触过的List、Diationary等,
试着将他们相互转换。以上仅供参考,方案并非最佳,希望能帮助大家!谢谢阅读与指正。