• C#泛型集合之Dictionary<k, v>使用技巧


    1. 1、要使用Dictionary集合,需要导入C#泛型命名空间
    2.   System.Collections.Generic(程序集:mscorlib)
    3. 2、描述
    4.    1)、从一组键(Key)到一组值(Value)的映射,每一个添加项都是由一个值及其相关连的键组成
    5.    2)、任何键都必须是唯一的
    6.    3)、键不能为空引用null(VB中的Nothing),若值为引用类型,则可以为空值
    7.    4)、Key和Value可以是任何类型(string,int,custom class 等)
    8. 3、创建及初始化
    9.    Dictionary<int, string> myDictionary = new Dictionary<int, string>();
    10. 4、添加元素
    11.    myDictionary.Add("C#",0);
    12.    myDictionary.Add("C++",1);
    13.    myDictionary.Add("C",2);
    14.    myDictionary.Add("VB",2);
    15. 5、查找元素By Key
    16.   if(myDictionary.ContainsKey("C#"))
    17.   {
    18.     Console.WriteLine("Key:{0},Value:{1}", "C#", myDictionary["C#"]);
    19.   }
    20. 6.遍历元素 By KeyValuePair
    21.   foreach (KeyValuePair<string, int> kvp in myDictionary)
    22.   {
    23.     Console.WriteLine("Key = {0}, Value = {1}",kvp.Key, kvp.Value);
    24.   }
    25. 7、仅遍历键 By Keys 属性
    26.   Dictionary<string, int>.KeyCollection keyCol = myDictionary.Keys;
    27.   foreach (string key in keyCol/*string key in myDictionary.Keys*/)
    28.   {
    29.     Console.WriteLine("Key = {0}", key);
    30.   }
    31. 8、仅遍历值By Valus属性
    32.   Dictionary<string, int>.ValueCollection valueCol = myDictionary.Values;
    33.   foreach (int value in valueCol)
    34.   {
    35.     Console.WriteLine("Value = {0}", value);
    36.   }
    37. 9.移除指定的键值By Remove方法
    38.   myDictionary.Remove("C#");
    39.   if (myDictionary.ContainsKey("C#"))
    40.   {
    41.     Console.WriteLine("Key:{0},Value:{1}", "C#", myDictionary["C#"]);
    42.   }
    43.   else
    44.   {
    45.     Console.WriteLine("不存在 Key : C#");
    46. }
    47. 在System.Collections.Generic命名空间中,与ArrayList相对应的泛型集合是List<T>,与 HashTable相对应的泛型集合是Dictionary<K,V>,其存储数据的方式与哈希表相似,通过键/值来保存元素,并具有泛型的全部特征,编译时检查类型约束,读取时无须类型转换。
    48.   电话本存储的例子中,使用Dictionary<K,V>来存储电话本信息,代码如下:
    49. Dictionary<string,TelNote> ht=new Dictionary<string,TelNote>();
    50.   在Dictionary<K,V>声明中,“<string,TelNote>”中的string表示集合中Key的类型,TelNote表示Value的类型,定义Dictionary<K,V>泛型集合中的方法如下:
    51. Dictionary<K,V> students=new Dictionary<K,V>();
    52.   其中“K”为占位符,具体定义时用存储键“Key”的数据类型代替,“V”也是占位符,用元素的值“Value”的数据类型代替,这样就在定义该集合时,声明了存储元素的键和值的数据类型,保证了类型的安全性。
    53.   Dictionary<K,V>中元素的操作方法与HashTable相似,添加元素,获取元素,删除元素,遍历集合元素的方法基本相同。
    54. Dictionary<K,V>和HashTable的区别
    55. Dictionary<K,V>和HashTable的相同点:添加元素,删除元素,通过键访问值的方法相同。
    56. Dictionary<K,V>和HashTable的不同点:
    57. Dictionary<K,V>对添加的元素具有类型约束,HashTable可添加任意类型的元素。
    58. Dictionary<K,V>不需要装箱、拆箱操作,HashTable添加时装箱,读取时拆箱。
    59.   在Dictionary<K,V>集合中,除了通过键获取值的方法外,还有一种TryGetValue(key)方法,可以通过键获取值,该方法返回值为布尔型,如果存在和键相对应的值,则返回true,否则返回false。避免了因获取不到相应值发生的异常。
    60. using System;
    61. using System.Collections.Generic;
    62. class Program
    63. {
    64. static void Main()
    65. {
    66. //创建Dictionary<K,V>,然后添加元素
    67. Dictionary < string, string > film = new Dictionary < string, string > ();
    68. film.Add("韦小宝", "鹿鼎记");
    69. film.Add("陆小凤", "陆小凤传奇");
    70. film.Add("张无忌", "倚天屠龙记");
    71. film.Add("杨过", "神雕侠侣");
    72. film.Add("令狐冲", "笑傲江湖");
    73. Console.WriteLine("集合现在的元素个数为{0}", film.Count);
    74. film.Remove("杨过");
    75. //遍历集合
    76. Console.WriteLine("武侠电影的主角及电影名");
    77. Console.WriteLine("/t主角/t电影");
    78. foreach (KeyValuePair < string, string > kvp in film)
    79. {
    80. Console.WriteLine("/t{0}/t{1}", kvp.Key, kvp.Value);
    81. }
    82. //检查元素是否存在,如不存在,添加
    83. if (!film.ContainsKey("段誉"))
    84. {
    85. film.Add("段誉", "天龙八部");
    86. }
    87. //获取键的集合
    88. Dictionary < string, string > .KeyCollection keys = film.Keys;
    89. //遍历键的集合
    90. Console.WriteLine("受欢迎的武侠片中主角名");
    91. foreach (string str in keys)
    92. {
    93. Console.WriteLine(str);
    94. }
    95. Dictionary < string, string > .ValueCollection values = film.Values;
    96. //遍历值的集合
    97. Console.WriteLine("最受欢迎的武侠片");
    98. foreach (string strfilm in values)
    99. {
    100. Console.WriteLine(strfilm);
    101. }
    102. //遍历元素的另一种方法
    103. Console.WriteLine("和哈希表相同的遍历元素方法");
    104. foreach (string strname in film.Values)
    105. {
    106. Console.WriteLine(strname);
    107. }
    108. //获取键对应的值
    109. string myfilm = film["令狐冲"];
    110. Console.WriteLine("主角为令狐冲的电影名{0}", myfilm);
    111. //获取键对应值的TryGetValue方法
    112. string objfilm = string.Empty;
    113. if (film.TryGetValue("段誉", out objfilm))
    114. {
    115. Console.WriteLine("主角为段誉的电影是{0}", objfilm);
    116. }
    117. else
    118. Console.WriteLine("没有主角为段誉的电影");
    119. Console.ReadKey();
    120. }
    121. }
    122.   代码创建了一个Dictionary<K,V>集合,键和值的数据类型是string类型,后边代码的元素添加,删除都和哈希表处理方法相同,遍历元素时不需要进行数据类型强制转换。Dictionary<K,V>通过键取值的TryGetValue方法,此方法包括两个参数,一个是要查询的键,另一个是获取的值,注意值前面使用out关键字。
    123. 注意:使用TryGetValue方法时,参数一定要使用out关键字,否则编译失败。
    124. 原文链接:http://www.code-design.cn/article/20120210/csharp-generic-Collection-Dictionary.aspx
  • 相关阅读:
    Oracle查询今天的数据(昨天、本周...)
    Windows添加删除 route
    大三寒假学习进度(九)
    大三寒假学习进度(八)
    大三寒假学习进度(七)
    大三寒假学习进度(六)
    大三寒假学习进度(五)
    《软件架构师应该知道的97件事》阅读笔记(一)
    大三寒假学习进度(四)
    大三寒假学习进度(三)
  • 原文地址:https://www.cnblogs.com/huibin-benteng/p/5132619.html
Copyright © 2020-2023  润新知