1. 程式人生 > 其它 >裝箱、拆箱、hashtable集合

裝箱、拆箱、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]); }