CS4L8——Dictionary<>
本章代码关键字
1 2 3 4 5 6 7 8 9 10 11
| Dictionary<> dictionary<>.Add() dictionary<>.Remove() dictionary<>.Clear() dictionary<>[] dictionary<>.ContainsKey() dictionary<>.ContainsValue() dictionary<>.Count dictionary<>.Keys dictionary<>.Values KeyValuePair<>
|
Dictionary<>
可以将 Dictionary<>
理解为 拥有泛型的 Hashtable
它也是基于键的哈希代码组织起来的 键/值对
键值对类型从 Hashtable
的 object
变成了可以自己指定的泛型
使用 Dictionary<>
需要引用 System.Collections.Generic
这个命名空间
实例化一个 Dictionary<>
必须要指明它要装载什么类型的元素
1 2 3 4 5 6 7 8 9 10 11
| using System.Collections.Generic;
class Program { static void Main(string[] args) { List<int> list = new List<int>(); List<string> list2 = new List<string>(); List<bool> list3 = new List<bool>(); } }
|
增删查改
增
注意:添加键值对不能重复添加键,如果重复添加键会报错(但可以有相同值)
1 2 3 4 5
| Dictionary<int, string> dictionary = new Dictionary<int, string>(); dictionary.Add(1, "123"); dictionary.Add(2, "222"); dictionary.Add(3, "222");
|
删
只能通过键去删除,删除不存在的键,会没有反应
1 2 3 4 5 6
| Dictionary<int, string> dictionary = new Dictionary<int, string>(); dictionary.Add(1, "123"); dictionary.Add(2, "222"); dictionary.Remove(1);
dictionary.Remove(4);
|
清空
1 2 3 4
| Dictionary<int, string> dictionary = new Dictionary<int, string>(); dictionary.Add(1, "123"); dictionary.Add(2, "222"); dictionary.Clear();
|
查
索引器通过键获取值,找不到键会直接报错!!!
1 2 3 4 5 6 7
| Dictionary<int, string> dictionary = new Dictionary<int, string>(); dictionary.Add(1, "123"); dictionary.Add(2, "222"); Console.WriteLine(dictionary[1]); Console.WriteLine(dictionary[2]);
|
输出:
检测键是否存在
1 2 3 4 5 6 7
| Dictionary<int, string> dictionary = new Dictionary<int, string>(); dictionary.Add(1, "123"); dictionary.Add(2, "222"); if (dictionary.ContainsKey(1)) { Console.WriteLine("存在键为1的键值对"); }
|
输出:
检测值是否存在
1 2 3 4 5 6 7
| Dictionary<int, string> dictionary = new Dictionary<int, string>(); dictionary.Add(1, "123"); dictionary.Add(2, "222"); if (dictionary.ContainsValue("123")) { Console.WriteLine("存在值为123的键值对"); }
|
输出:
改
通过索引器修改值,只能改键对应的值,无法修改值对应的键
1 2 3 4 5
| Dictionary<int, string> dictionary = new Dictionary<int, string>(); dictionary.Add(1, "123"); Console.WriteLine(dictionary[1]); dictionary[1] = "555"; Console.WriteLine(dictionary[1]);
|
输出:
遍历
得到键值对对数
1 2 3 4
| Dictionary<int, string> dictionary = new Dictionary<int, string>(); dictionary.Add(1, "123"); dictionary.Add(2, "222"); Console.WriteLine(dictionary.Count);
|
输出:
foreach 遍历所有键
注意!遍历用的临时变量要变成 Dictionary
指定的类型,才能用临时变量获取值
1 2 3 4 5 6 7 8 9 10
| Dictionary<int, string> dictionary = new Dictionary<int, string>(); dictionary.Add(1, "123"); dictionary.Add(2, "222"); dictionary.Add(3, "222"); foreach (int key in dictionary.Keys) { Console.WriteLine("键:" + key); Console.WriteLine("值:" + dictionary[key]); }
|
输出:
1 2 3 4 5 6
| 键:1 值:123 键:2 值:222 键:3 值:222
|
遍历所有值
1 2 3 4 5 6 7 8
| Dictionary<int, string> dictionary = new Dictionary<int, string>(); dictionary.Add(1, "123"); dictionary.Add(2, "222"); dictionary.Add(3, "222"); foreach (string value in dictionary.Values) { Console.WriteLine("值:" + value); }
|
输出:
键值对一起遍历
用 KeyValuePair<键类型, 值类型>
来作为临时变量类型
1 2 3 4 5 6 7 8
| Dictionary<int, string> dictionary = new Dictionary<int, string>(); dictionary.Add(1, "123"); dictionary.Add(2, "222"); dictionary.Add(3, "222"); foreach (KeyValuePair<int, string> item in dictionary) { Console.WriteLine("键:" + item.Key + ",值:" + item.Value); }
|
输出:
1 2 3
| 键:1,值:123 键:2,值:222 键:3,值:222
|