装箱、拆箱、hashtable集合
1、装箱、拆箱
1)装箱用的什么类型,拆箱的时候也用什么类型
int n = 10; object o= n; int nn=(int)o; Console.WriteLine(nn);
2、hashtable:键---值集合
字典:拼音----->汉字 //键---->值 //创建了一个键值对集合 Hashtable ht=new Hashtable(); ht.Add("a", 1); ht.Add("b", 2); ht.Add("男",false); ht.Add("80kg", 178.99); //键必须是唯一的,值是可以重复的 ht.Add("c", 1); //给值重新赋值 ht["c"] = 3; //判断是否包含键 if (!ht.ContainsKey("男")) { ht.Add ("男", true); } else { Console.WriteLine("已经包含相同键"); } //清空集合 ht.Clear(); foreach(var itme in ht.Keys) { Console.WriteLine("键-----{0},值{1}",itme,ht[itme]); } Console.WriteLine(ht);
3、统计welcome to China 中的每个字符出现的次数
//统计welcome to China 中的每个字符出现的次数 string str = "welcom to china"; //字符------->出现的次数 Hashtable ht = new Hashtable(); for(int i = 0; i < str.Length; i++) { //为空是,跳过 if (str[i] == ' ') { continue; } //相同字符出现,+1 if (!ht.ContainsKey(str[i])) { //该字母在集合中第一次出现 ht.Add(str[i], 1); } else { //字母在键中出现过一次,值++ int n = (int)ht[str[i]]; n++; ht[str[i]] = n; } } foreach (var itme in ht.Keys) { Console.WriteLine("字母{0}出现了{1}次", itme, ht[itme]); }