using System;
using System.Collections; // 集合
using System.Collections.Generic; // 泛型集合
namespace 集合之HashTable
{
class Person {
public string user_Name;
public int user_Id;
}
class MainClass
{
public static void Main (string[] args)
{
// 為什么要引入集合概念:
// 演示數(shù)組的局限性
// 1.數(shù)組的局限性
// 數(shù)組只能存儲相同類型的數(shù)據(jù),比如int[] 數(shù)組只能存儲int數(shù)據(jù)
// 2.數(shù)組存在大量垃圾數(shù)據(jù)
// string[] strs = new string[2];
// strs[0] = "張三";
// strs[1] = "張三";
// foreach (string item in strs) {
// Console.WriteLine (item);
// }
// 3.數(shù)組不能動(dòng)態(tài)的擴(kuò)展長度
// 集合
// Hashtable;
// ArrayList;
// Stack;
// Queue;
// 泛型集合
// ArrayList --> List<T>
// ArrayList al = new ArrayList();
// List<int> li = new List<int>();
// Hashtable --> Dictionary<Type,Type>
// HashTable
// Key -> value
// Key2 -> value
// 這里的key是唯一的
// Hashtable ht = new Hashtable();
// ht.Add (001,"老王");
// ht.Add (002,"老王2");
// ht.Add (003,"老王3");
// // 通過key取value
// Console.WriteLine (ht[001]);
//
// // 通過一個(gè)key移除一個(gè)value值
// ht.Remove(001);
//
// // 全部移除
// ht.Clear();
// 查找Key || value
// if (ht.Contains(001)) {
// Console.WriteLine ("True");
// }
//
// if (ht.ContainsKey(001)) {
// Console.WriteLine ("True");
// }
// if (ht.ContainsValue("老王")) {
// Console.WriteLine ("True");
// }
// // 遍歷所有的key
// foreach (var item in ht.Keys) {
// Console.WriteLine (item);
// }
//
// // 遍歷所有的value
// foreach (var item in ht.Values) {
// Console.WriteLine (item);
// }
// 遍歷所有key - value
Hashtable ht = new Hashtable();
Person p1 = new Person();
p1.user_Name = "老王";
p1.user_Id = 1001;
Person p2 = new Person();
p2.user_Name = "老張";
p2.user_Id = 1002;
ht.
ht.Add (p1.user_Id, p1);
ht.Add (p2.user_Id, p2);
// 獲取字段枚舉遍歷器
IDictionaryEnumerator ide = ht.GetEnumerator();
while (ide.MoveNext()) {
// ide.Entry;
// 將取出的內(nèi)容轉(zhuǎn)換成字典實(shí)體
DictionaryEntry de = (DictionaryEntry)ide.Current;
// 通過字典實(shí)體內(nèi)的字段value 取出當(dāng)時(shí)你存value時(shí)候的對象P,取出來的對象
// 需要轉(zhuǎn)換(和你當(dāng)時(shí)存的value類型是一致的)
Person per = de.Value as Person;
Console.WriteLine ("用戶的id:{0},用戶的姓名:{1}",per.user_Id,per.user_Name);
}
}
}
}