CS4L4——Hashtable
本章代码关键字
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 
 | Hashtable                    hashtable.Add()
 hashtable.Remove()
 hashtable.Clear()
 hashtable[]
 hashtable.Contains()
 hashtable.ContainsKey()
 hashtable.ContainsValue()
 hashtable.Count
 Hashtable.Keys
 hashtable.Values
 hashtable.GetEnumerator()
 
 | 
Hashtable
Hashtable(又称散列表)是基于键的哈希代码组织起来的 键/值对
它的主要作用是提高数据查询的效率,使用键来访问集合中的元素
使用 Hashtable 需要引用 System.Collections 这个命名空间
| 12
 3
 4
 5
 6
 7
 8
 9
 
 | using System.Collections;
 class Program
 {
 static void Main(string[] args)
 {
 Hashtable hashtable = new Hashtable();
 }
 }
 
 | 
增删查改
增
键与值都是 object 类型,可以添加任意 object 类型
| 12
 3
 4
 5
 
 | Hashtable hashtable = new Hashtable();hashtable.Add(1, "123");
 hashtable.Add("123", 2);
 hashtable.Add(true, false);
 hashtable.Add(false, true);
 
 | 
删
只能通过键去删除,删除不存在的键,会没有反应
| 12
 3
 4
 
 | Hashtable hashtable = new Hashtable();hashtable.Add(1, "123");
 hashtable.Remove(1);
 hashtable.Remove(2);
 
 | 
清空
清空哈希表
| 12
 3
 4
 5
 6
 
 | Hashtable hashtable = new Hashtable();hashtable.Add(1, "123");
 hashtable.Add("123", 2);
 hashtable.Add(true, false);
 hashtable.Add(false, true);
 hashtable.Clear();
 
 | 
查
使用索引器通过键查看值,找不到返回空 (null)
| 12
 3
 4
 5
 6
 7
 8
 
 | Hashtable hashtable = new Hashtable();hashtable.Add(1, "123");
 hashtable.Add(2, "1234");
 hashtable.Add("123123", 12);
 
 Console.WriteLine(hashtable[1]);
 Console.WriteLine(hashtable[0]);
 Console.WriteLine(hashtable["123123"]);
 
 | 
输出:
检测键或值是否存在于hashtable
hashtable.Contains() 和 hashtable.ContainsKey() 都是检测键是否存在
hashtable.ContainsValue() 是检测值是否存在
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 
 | Hashtable hashtable = new Hashtable();hashtable.Add(1, "123");
 hashtable.Add(2, "1234");
 hashtable.Add("123123", 12);
 
 if (hashtable.Contains(2))
 {
 Console.WriteLine("存在键为2的键值对");
 }
 
 if (hashtable.ContainsKey(2))
 {
 Console.WriteLine("存在键为2的键值对");
 }
 
 
 if (hashtable.ContainsValue(12))
 {
 Console.WriteLine("存在值为12的键值对");
 }
 
 | 
输出:
| 12
 3
 
 | 存在键为2的键值对存在键为2的键值对
 存在值为12的键值对
 
 | 
改
只能通过索引器改键对应的值内容,无法修改键
| 12
 3
 4
 5
 
 | Hashtable hashtable = new Hashtable();hashtable.Add(1, "123");
 Console.WriteLine(hashtable[1]);
 hashtable[1] = 100.5f;
 Console.WriteLine(hashtable[1]);
 
 | 
输出:
遍历
得到键值对对数
| 12
 3
 4
 5
 6
 
 | Hashtable hashtable = new Hashtable();hashtable.Add(1, "123");
 hashtable.Add(2, "1234");
 hashtable.Add("123123", 12);
 
 Console.WriteLine(hashtable.Count);
 
 | 
输出:
遍历所有键
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 
 | Hashtable hashtable = new Hashtable();hashtable.Add(1, "123");
 hashtable.Add(2, "1234");
 hashtable.Add("123123", 12);
 
 foreach (var key in hashtable.Keys)
 {
 Console.WriteLine("键:" + key);
 Console.WriteLine("值:" + hashtable[key]);
 }
 
 | 
输出:
| 12
 3
 4
 5
 6
 
 | 键:123123值:12
 键:2
 值:1234
 键:1
 值:123
 
 | 
遍历所有值
hashtable.Values 不存在索引器,不能直接用 for 循环遍历
| 12
 3
 4
 5
 6
 7
 8
 9
 
 | Hashtable hashtable = new Hashtable();hashtable.Add(1, "123");
 hashtable.Add(2, "1234");
 hashtable.Add("123123", 12);
 
 foreach (var value in hashtable.Values)
 {
 Console.WriteLine("值:" + value);
 }
 
 | 
输出:
键值对一起遍历
| 12
 3
 4
 5
 6
 7
 8
 9
 
 | Hashtable hashtable = new Hashtable();hashtable.Add(1, "123");
 hashtable.Add(2, "1234");
 hashtable.Add("123123", 12);
 
 foreach (DictionaryEntry item in hashtable)
 {
 Console.WriteLine("键:" + item.Key + ",值:" + item.Value);
 }
 
 | 
输出:
| 12
 3
 
 | 键:123123,值:12键:2,值:1234
 键:1,值:123
 
 | 
迭代器遍历法
获取 迭代器 后,循环迭代,每次获取到一个值后就迭代
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 
 | Hashtable hashtable = new Hashtable();hashtable.Add(1, "123");
 hashtable.Add(2, "1234");
 hashtable.Add("123123", 12);
 
 IDictionaryEnumerator enumerator = hashtable.GetEnumerator();
 bool flag = enumerator.MoveNext();
 while (flag)
 {
 Console.WriteLine("键:" + enumerator.Key + ",值:" + enumerator.Value);
 flag = enumerator.MoveNext();
 }
 
 | 
输出:
| 12
 3
 
 | 键:123123,值:12键:2,值:1234
 键:1,值:123
 
 | 
装箱拆箱
由于用万物之父 object 来存储数据,自然存在装箱拆箱,
当往其中进行值类型存储时就是在装箱,将值类型对象取出来转换使用时,就是在拆箱