HashTable Dictionary

遍历 HashTable Dictionary 

View Code
 1 Dictionary<int, string> food = new Dictionary<int, string>();   // HashTable<int, string> food = new HashTable<int, string>();
 2 food.Add(1, "A");
 3 food.Add(2, "B");
 4 IDictionaryEnumerator myEnumerator=food.GetEnumerator();
 5 string strfoodlist = "";
 6 while (myEnumerator.MoveNext())
 7 {
 8  strfoodlist = strfoodlist + "\n\n" + myEnumerator.Key.ToString();
 9  strfoodlist = strfoodlist + ":\t" + myEnumerator.Value.ToString();
10 }
11 Console.WriteLine(strfoodlist);

排序 HashTable Dictionary

View Code
 1  static void Main(string[] args)
 2         {
 3             Console.WriteLine();
 4             Console.WriteLine("Hashtable:");
 5             Hashtable ht = new Hashtable();
 6             ht.Add("321", "fdsgsags");
 7             ht.Add("acb", "test test");
 8             ht.Add("1123", "lslgsgl");
 9             ht.Add("2bcd13", "value");
10             foreach (DictionaryEntry de in ht)
11             {
12                 Console.WriteLine("键名:" + de.Key + " 键值:" + de.Value);
13             }
14 
15             Console.WriteLine();
16             Console.WriteLine("Dictionary:");
17             Dictionary<string, string> dic = new Dictionary<string, string>();
18             dic.Add("321", "fdsgsags");
19             dic.Add("acb", "test test");
20             dic.Add("1123", "lslgsgl");
21             dic.Add("2bcd13", "value");
22             foreach (KeyValuePair<string, string> item in dic)
23             {
24                 Console.WriteLine("键名:" + item.Key + " 键值:" + item.Value);
25             }
26 
27             Console.WriteLine();
28             Console.WriteLine("ArrayList:");
29             ArrayList al = new ArrayList(dic.Keys); //或者 ArrayList al = new ArrayList(ht.Keys); 
30             al.Sort();
31             foreach (string skey in al)
32             {
33                 Console.WriteLine("键名:" + skey + " 键值:" + dic[skey]); //或者 Console.WriteLine("键名:" + skey + " 键值:" + ht[skey]);
34             }
35 
36             Console.Read();
37         }

复制 Dictionary

View Code
1 Dictionary<string, int> copy = new Dictionary<string, int>(OldDic);

 

 

 

 

 

 

posted @ 2012-05-07 16:11  一篮饭特稀  阅读(308)  评论(0编辑  收藏  举报