八、Dictionary
1. Dictionary的本质
可以将Dictionary理解为拥有泛型的Hashtable。它也是基于键的哈希代码组织起来的键值对。键值对类型从Hashtable的object变为可以自己制定的泛型。
2. 声明
需要引用命名空间using System.Collections.Generic
Dictionary dictionary = new Dictionary();
3. 增删查改
增
dictionary.Add(1, "123"); //不能出现相同键,可以出现相同值
删
(1)只能通过键去删除,删除不存在的键没反应
dictionary.Remove(1);
(2)清空
dictionary.Clear();
查
(1)通过键查看值,找不到直接报错
Console.WriteLine(dictionary[2]);
(2)查看是否存在
根据键检测
if(dictionary.ContainsKey(1))
{
Console.WriteLine("存在键为1的键值对");
}
根据值检测
if(dictionary.ContainsValue("123"))
{
Console.WriteLine("存在值为123的键值对");
}
改
dictionary[1] = "555";
4. 遍历
(1)遍历所有键
foreach (int item in dictionary.Keys)
{
Console.WriteLine(item);
Console.WriteLine(dictionary[item]);
}
(2)遍历所有值
foreach (string item in dictionary.Values)
{
Console.WriteLine(item);
}
(3)键值对一起遍历
foreach (KeyValuePair item in dictionary)
{
Console.WriteLine("键:" + item.Key + "值:" + item.Value);
}
1. Dictionary的本质
可以将Dictionary理解为拥有泛型的Hashtable。它也是基于键的哈希代码组织起来的键值对。键值对类型从Hashtable的object变为可以自己制定的泛型。
2. 声明
需要引用命名空间using System.Collections.Generic
Dictionary
3. 增删查改
增
dictionary.Add(1, "123"); //不能出现相同键,可以出现相同值
删
(1)只能通过键去删除,删除不存在的键没反应
dictionary.Remove(1);
(2)清空
dictionary.Clear();
查
(1)通过键查看值,找不到直接报错
Console.WriteLine(dictionary[2]);
(2)查看是否存在
根据键检测
if(dictionary.ContainsKey(1))
{
Console.WriteLine("存在键为1的键值对");
}
根据值检测
if(dictionary.ContainsValue("123"))
{
Console.WriteLine("存在值为123的键值对");
}
改
dictionary[1] = "555";
4. 遍历
(1)遍历所有键
foreach (int item in dictionary.Keys)
{
Console.WriteLine(item);
Console.WriteLine(dictionary[item]);
}
(2)遍历所有值
foreach (string item in dictionary.Values)
{
Console.WriteLine(item);
}
(3)键值对一起遍历
foreach (KeyValuePair
{
Console.WriteLine("键:" + item.Key + "值:" + item.Value);
}
Give your staff fun and unique ways to celebrate China’s most important holiday: Spring Festival! Through our products, we at Cultural Keys hope to inspire everyone to take a greater interest in and develop a deeper understanding of and respect for traditional Chinese culture and through that process, learn how to make deeper connections with China and its people.
This is crucial for any organisation with dealings with China. And there’s no better time to do this than at Spring Festival!
Cultural Keys helps people better access, experience and enjoy traditional Chinese culture, through a range of products, services, content and experiences. Stay updated on all our news, photos, events and activities related to Chinese culture by following us on social media!
Facebook: CulturalKeysChina
Instagram: CulturalKeys
LinkedIn: Cultural Keys Chinese Culture Company
Twitter: CulturalKeysCN
WeChat Official Account: CulturalKeysCK
#beijing #culture #beijingevents #teambuilding
https://t.cn/A69LZi71
This is crucial for any organisation with dealings with China. And there’s no better time to do this than at Spring Festival!
Cultural Keys helps people better access, experience and enjoy traditional Chinese culture, through a range of products, services, content and experiences. Stay updated on all our news, photos, events and activities related to Chinese culture by following us on social media!
Facebook: CulturalKeysChina
Instagram: CulturalKeys
LinkedIn: Cultural Keys Chinese Culture Company
Twitter: CulturalKeysCN
WeChat Official Account: CulturalKeysCK
#beijing #culture #beijingevents #teambuilding
https://t.cn/A69LZi71
Boot Key
Island in the middle Florida Keys, United States
Boot Key is an island in the middle Florida Keys located adjacent to Key Vaca. Boot Key is within the city limits of Marathon, Florida, United States. The island is largely undeveloped. A draw bridge that once connected the island to Key Vaca was demolished by the city in 2010 after failing FDOT's inspection.
#Florida ##UnitedStates#
"Boot Key has been protected from development since 2011 by NOAA’s Coastal & Estuarine Land Conservation Program."
Immediately adjacent to Boot Key is a 38-acre (15 ha) tract of land from which the United States Government broadcasts Radio Marti. These broadcasts consist of anti-communist news and information directed towards residents of Cuba, in an attempt to support opposition against the Cuban government.
A hawk watch is conducted every fall on Boot Key.
Education
It is in the Monroe County School District. It is zoned to Stanley Switick Elementary School (K-8) in Marathon.
#Marathon ##Island#
Island in the middle Florida Keys, United States
Boot Key is an island in the middle Florida Keys located adjacent to Key Vaca. Boot Key is within the city limits of Marathon, Florida, United States. The island is largely undeveloped. A draw bridge that once connected the island to Key Vaca was demolished by the city in 2010 after failing FDOT's inspection.
#Florida ##UnitedStates#
"Boot Key has been protected from development since 2011 by NOAA’s Coastal & Estuarine Land Conservation Program."
Immediately adjacent to Boot Key is a 38-acre (15 ha) tract of land from which the United States Government broadcasts Radio Marti. These broadcasts consist of anti-communist news and information directed towards residents of Cuba, in an attempt to support opposition against the Cuban government.
A hawk watch is conducted every fall on Boot Key.
Education
It is in the Monroe County School District. It is zoned to Stanley Switick Elementary School (K-8) in Marathon.
#Marathon ##Island#
✋热门推荐