Dictionary 概述
-
Dictionary<TKey, TValue>
存储的是键值对(Key - Value),通过键(Key)来存储或修改值(Value) -
Dictionary<TKey, TValue>
存储的键值对是无序的 -
Dictionary<TKey, TValue>
存储的键是不可重复的 -
Dictionary<TKey, TValue>
支持泛型,可以指定存储的键值对的类型 -
Dictionary<TKey, TValue>
不是线程安全的,在多线程环境中需要谨慎使用
一、Dictionary 的定义
- 定义 Dictionary
Dictionary<string, int> ageDict = new Dictionary<string, int>();
- 定义 Dictionary 并填充一些元素
Dictionary<string, int> ageDict = new Dictionary<string, int>
{
{ "Alice", 30},
{ "Bob", 25}
};
二、Dictionary 元素的基本操作
1、Dictionary 元素的添加
- Dictionary 元素的添加(标准方式)
Dictionary<string, int> ageDict = new Dictionary<string, int>();
ageDict.Add("Alice", 30);
ageDict.Add("Bob", 25);
- Dictionary 元素的添加(其他方式)
Dictionary<string, int> ageDict = new Dictionary<string, int>();
ageDict["Alice"] = 30;
ageDict["Bob"] = 25;
2、Dictionary 元素的访问
Dictionary<string, int> ageDict = new Dictionary<string, int>();
ageDict.Add("Alice", 30);
ageDict.Add("Bob", 25);
Console.WriteLine(ageDict["Alice"]);
Console.WriteLine(ageDict["Bob"]);
# 输出结果
30
25
3、Dictionary 元素的修改
Dictionary<string, int> ageDict = new Dictionary<string, int>();
ageDict.Add("Alice", 30);
ageDict.Add("Bob", 25);
ageDict["Alice"] = 31;
ageDict["Bob"] = 26;
Console.WriteLine(ageDict["Alice"]);
Console.WriteLine(ageDict["Bob"]);
# 输出结果
31
26
三、Dictionary 元素的遍历
1、使用 for 循环遍历键值对
Dictionary<string, int> ageDict = new Dictionary<string, int>();
ageDict.Add("Alice", 30);
ageDict.Add("Bob", 25);
foreach (KeyValuePair<string, int> kvp in ageDict)
{
Console.WriteLine($"{kvp.Key} - {kvp.Value}");
}
# 输出结果
Alice - 30
Bob - 25
2、使用 for 循环遍历键
Dictionary<string, int> ageDict = new Dictionary<string, int>();
ageDict.Add("Alice", 30);
ageDict.Add("Bob", 25);
foreach (string key in ageDict.Keys)
{
Console.WriteLine(key);
}
# 输出结果
Alice
Bob
3、使用 for 循环遍历值
Dictionary<string, int> ageDict = new Dictionary<string, int>();
ageDict.Add("Alice", 30);
ageDict.Add("Bob", 25);
foreach (int value in ageDict.Values)
{
Console.WriteLine(value);
}
# 输出结果
30
25
四、Dictionary 的常用方法
1、Dictionary 元素的删除
Dictionary<string, int> ageDict = new Dictionary<string, int>();
ageDict.Add("Alice", 30);
ageDict.Add("Bob", 25);
Console.WriteLine("删除元素之前");
foreach (KeyValuePair<string, int> kvp in ageDict)
{
Console.WriteLine(kvp.Key);
Console.WriteLine(kvp.Value);
}
Console.WriteLine("删除元素之后");
ageDict.Remove("Bob");
foreach (KeyValuePair<string, int> kvp in ageDict)
{
Console.WriteLine(kvp.Key);
Console.WriteLine(kvp.Value);
}
# 输出结果
删除元素之前
Alice
30
Bob
25
删除元素之后
Alice
30
2、Dictionary 元素的检测
- Dictionary 元素的检测(Key)
Dictionary<string, int> ageDict = new Dictionary<string, int>();
ageDict.Add("Alice", 30);
ageDict.Add("Bob", 25);
Console.WriteLine(ageDict.ContainsKey("Alice"));
Console.WriteLine(ageDict.ContainsKey("Jack"));
# 输出结果
True
False
- Dictionary 元素的检测(Value)
Dictionary<string, int> ageDict = new Dictionary<string, int>();
ageDict.Add("Alice", 30);
ageDict.Add("Bob", 25);
Console.WriteLine(ageDict.ContainsValue(20));
Console.WriteLine(ageDict.ContainsValue(25));
# 输出结果
False
True
3、Dictionary 大小的获取
Dictionary<string, int> ageDict = new Dictionary<string, int>();
ageDict.Add("Alice", 30);
ageDict.Add("Bob", 25);
Console.WriteLine(ageDict.Count);
# 输出结果
2
4、Dictionary 元素的清空
Dictionary<string, int> ageDict = new Dictionary<string, int>();
ageDict.Add("Alice", 30);
ageDict.Add("Bob", 25);
Console.WriteLine("清空元素之前");
foreach (KeyValuePair<string, int> kvp in ageDict)
{
Console.WriteLine(kvp.Key);
Console.WriteLine(kvp.Value);
}
Console.WriteLine("清空元素之后");
ageDict.Clear();
foreach (KeyValuePair<string, int> kvp in ageDict)
{
Console.WriteLine(kvp.Key);
Console.WriteLine(kvp.Value);
}
# 输出结果
清空元素之前
Alice
30
Bob
25
清空元素之后
标签:Console,Dictionary,元素,Alice,ageDict,Add,WriteLine,基本操作
From: https://blog.csdn.net/weixin_52173250/article/details/143657996