码迷,mamicode.com
首页 > 其他好文 > 详细

C#集合之Hashtable

时间:2014-07-21 11:03:12      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   使用   strong   os   

   Hashtable是一个键值对集合,其泛型版本是Dictionary<K, V>,下面说下常用的一些方法;

1.Add(),向Hashtable添加元素,需要注意的是因为它是键值对集合,所以要同时添加一个键和一个值,且键不能重复。

2.ContainsKey(key)方法,是查询是否包含某个键,其Contains()方法最终也是调用这个方法实现的。

3.ContainsValue(value)方法,是查询是否包含某个值。

4.hash[“key”],根据键获取某个值。hash[“key”]=“修改”,根据键修改对应的值。

5.Remove(“key”);根据键删除对应的值。

需要注意的是,以上3个方法,其泛型版本Dictionary<K, V>也是一样的。

Keys属性,是Hashtable的键集合。Values属性,是Hashtable的值集合;可以通过foreach遍历它的键集合和值集合。

如果要同时得到Hashtable中的键集合和值集合,在使用foreach遍历时,请将var关键字替换为DictionaryEntry,这样就能同时拿到键和值了。

而对于Dictionary<K, V>要同时得到键集合和值集合,在使用foreach遍历时,请将var关键字替换为KeyValuePair<k,v>。

键值对集合,如果得到键,可以使用 集合名[键名]这样的索引方式获取对应的值。因为键值对集合不提供下标来访问,故这类集合不能通过for循环来遍历。

下面看几个例子程序,以便更好理解其实际应用:

1.将int数组中的奇数放到一个新的int数组中返回

int[] iArray = new int[] { 2, 7, 8, 3, 22, 9, 5, 11, 14, 18, 21 };
            ArrayList aLst = new ArrayList();
            for (int i = 0; i < iArray.Length; i++)
            {
                if (iArray[i] % 2 != 0)
                {
                    aLst.Add(iArray[i]);
                }
            }
            object[] obj = aLst.ToArray();
            foreach (var item in obj)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();

 2.从一个整数的List<int>中取出最大数(找最大值)(最简单是调用List集合的max方法)

 1 private static int GetMax(List<int> iList)
 2         {
 3             int iMax = iList[0];
 4             for (int i = 0; i < iList.Count; i++)
 5             {
 6                 if (iList[i]>iMax)
 7                 {
 8                     iMax = iList[i];
 9                 }
10             }
11             return iMax;
12         }

3.把123转换为:壹贰叁。

 1 /// <summary>
 2         /// 数字转换成大写汉字
 3         /// </summary>
 4         /// <param name="msg"></param>
 5         /// <returns></returns>
 6         private static string ConvertInt(string msg)
 7         {
 8             string s = "0零 1壹 2贰 3叁 4肆 5伍 6陆 7柒 8扒 9玖";
 9             Dictionary<char, char> dic = new Dictionary<char, char>();
10 
11             string[] strArray = s.Split( );
12             for (int i = 0; i < strArray.Length; i++)
13             {
14                 dic.Add(strArray[i][0], strArray[i][1]);
15             }
16 
17             StringBuilder sb = new StringBuilder();
18 
19             for (int i = 0; i < msg.Length; i++)
20             {
21                 if (dic.ContainsKey(msg[i]))
22                 {
23                     sb.Append(dic[msg[i]]);
24                 }
25                 else
26                 {
27                     sb.Append(msg[i]);
28                 }
29             }
30             return sb.ToString();
31         }

4.计算字符串中每种字母出现的次数(面试题)。 “Welcome ,to Chinaworld”

 1             Dictionary<char, int> dic = new Dictionary<char, int>();
 2 
 3             for (int i = 0; i < msg.Length; i++)
 4             {
 5                 if (!dic.ContainsKey(msg[i]))
 6                 {
 7                     dic.Add(msg[i], 1);
 8                 }
 9                 else
10                 {
11                     dic[msg[i]] = dic[msg[i]] + 1;
12                 }
13             }
14 
15             foreach (KeyValuePair<char, int> item in dic)
16             {
17                 Console.WriteLine("字符<{0}>在文本中出现了{1}次", item.Key, item.Value);
18             }

延伸,如果是要统计一段中文中,某个词组呢?

 1 string msg = "患者:“大夫,我咳嗽得很重。”     大夫:“你多大年记?”     患者:“七十五岁。”     大夫:“二十岁咳嗽吗”患者:“不咳嗽。”     大夫:“四十岁时咳嗽吗?”     患者:“也不咳嗽。”     大夫:“那现在不咳嗽,还要等到什么时咳嗽?”";
 2             string key = "咳嗽";
 3             int iIndex = msg.IndexOf(key, 0);
 4             if (iIndex!=-1)
 5             {
 6                 Console.WriteLine("字符{0}第1次出现的位置是:{1}", key, iIndex);
 7             }
 8             
 9             int iCount = 1;
10             while (iIndex != -1)
11             {
12                 iIndex = msg.IndexOf(key, iIndex + key.Length);
13                 if (iIndex != -1)
14                 {
15                     iCount++;
16                     Console.WriteLine("字符{0}第{1}次出现的位置是:{2}", key, iCount, iIndex);
17                 }
18                 else
19                 {
20                     break;
21                 }
22             }

5.编写一个函数进行日期转换,将输入的中文日期转换为阿拉伯数字日期,比如:二零一二年十二月月二十一日要转换为2012-12-21

 1 private static string ConvertDate(string strDate)
 2         {
 3             Dictionary<char, char> dic = new Dictionary<char, char>();
 4             string s = "零0 一1 二2 三3 四4 五5 六6 七7 八8 九9";
 5             string[] strArray = s.Split( );
 6             for (int i = 0; i < strArray.Length; i++)
 7             {
 8                 dic.Add(strArray[i][0], strArray[i][1]);
 9             }
10 
11             StringBuilder sb = new StringBuilder();
12             for (int j = 0; j < strDate.Length; j++)
13             {
14                 if (dic.ContainsKey(strDate[j]))
15                 {
16                     sb.Append(dic[strDate[j]]);
17                 }
18                 else
19                 {
20                     if (strDate[j]==)
21                     {
22                         //1.十  10    10
23                         //2.二十 20   0
24                         //3.十二 12   1
25                         //4.二十二 22   不翻译
26 
27                         if (!dic.ContainsKey(strDate[j - 1]) && !dic.ContainsKey(strDate[j + 1]))
28                         {
29                             sb.Append("10");
30                         }
31                         else if (dic.ContainsKey(strDate[j - 1]) && !dic.ContainsKey(strDate[j + 1]))
32                         {
33                             sb.Append("0");
34                         }
35                         else if (!dic.ContainsKey(strDate[j - 1]) && dic.ContainsKey(strDate[j + 1]))
36                         {
37                             sb.Append("1");
38                         }
39                         else if (dic.ContainsKey(strDate[j - 1]) && dic.ContainsKey(strDate[j + 1]))
40                         {
41 
42                         }
43                     }
44                     else
45                     {
46                         sb.Append(strDate[j]);
47                     }
48                 }
49             }
50             string n = sb.ToString().Replace(,-);
51             n = n.Replace(, -);
52             return n.Replace(, );
53         }

上面代码中的中文注释还对不同情况做了判断。

关于集合,先就说这么多,写出来算是给自己的总结,留作他日复习用。如果各路大神有补充,请跟我联系,共同进步啊!

C#集合之Hashtable,布布扣,bubuko.com

C#集合之Hashtable

标签:style   blog   color   使用   strong   os   

原文地址:http://www.cnblogs.com/chens2865/p/3857629.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!