本文实例讲述了C#实现自定义Dictionary类。分享给大家供大家参考。具体如下:
1.关于MyDictionary类
本文中实现的MyDictionary类具有如下功能
1)可以增加、修改、删除键值对
2)可以通过索引器,找到一个键对应的值
3)可以遍历打印类中全部的键值对
4)可以将类中的序列转化为有序的(不排序、升序、降序)List类型
MyDictionary类是一个具有两个参数的泛型类,内部机制采用以键值对(KeyValuePair)类型为元素的双向链表(LinkedList)实现
2.实现代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
class MyDictioinary<TKey, TValue> { LinkedList<KeyValuePair<TKey, TValue>> llDictionary = new LinkedList<KeyValuePair<TKey, TValue>>(); /// <summary> /// 索引器,输入TKey可以找到对应的TValue /// </summary> /// <param name="tk">键值</param> /// <returns></returns> public TValue this [TKey tk] { get { foreach (KeyValuePair<TKey, TValue> kvp in llDictionary) { if (tk.Equals(kvp.Key)) { return kvp.Value; } } return default (TValue); } } /// <summary> /// 添加一组数据 /// </summary> /// <param name="tk"></param> /// <param name="tv"></param> public void Add(TKey tk, TValue tv) { foreach (KeyValuePair<TKey, TValue> kvp in llDictionary) { if (tk.Equals(kvp.Key)) { throw new Exception( "新增失败:主键已经存在" ); } } llDictionary.AddLast( new KeyValuePair<TKey, TValue>(tk, tv)); } /// <summary> /// 删除一组数据 /// </summary> /// <param name="tk"></param> public void Delete(TKey tk) { foreach (KeyValuePair<TKey, TValue> kvp in llDictionary) { if (tk.Equals(kvp.Key)) { llDictionary.Remove(kvp); return ; } } throw new Exception( "删除失败:不存在这个主键" ); } /// <summary> /// 修改一组数据 /// </summary> /// <param name="tk"></param> /// <param name="tv"></param> public void Modify(TKey tk, TValue tv) { LinkedListNode<KeyValuePair<TKey, TValue>> lln = llDictionary.First; while (lln != null ) { if (tk.Equals(lln.Value.Key)) { llDictionary.AddBefore(lln, new KeyValuePair<TKey, TValue>(tk, tv)); llDictionary.Remove(lln); return ; } lln = lln.Next; } throw new Exception( "修改失败:不存在这个主键" ); } /// <summary> /// 打印整个Dictionary内的全部数据 /// </summary> public void PrintData() { Console.WriteLine( "==================" ); Console.WriteLine( "输出数据" ); Console.WriteLine( "==================" ); foreach (KeyValuePair<TKey, TValue> kvp in llDictionary) { Console.WriteLine(kvp.Key.ToString() + "\t:" + kvp.Value.ToString()); } Console.WriteLine( string .Format( "共计输出 {0} 项" , llDictionary.Count)); Console.WriteLine(DateTime.Now.ToLongTimeString()); Console.WriteLine( "==================" ); } /// <summary> /// 决定按什么顺序打印Dictionary内的数据 /// </summary> public enum PrintType { Normal = 0, //不排序 Ascend = 1, //升序排列 Descend = 2 //降序佩列 }; /// <summary> /// 返回List类型的数据 /// </summary> /// <param name="printType">List内数据的排序情况</param> /// <returns></returns> public List<KeyValuePair<TKey, TValue>> ToList(PrintType printType = PrintType.Normal) { switch (printType) { case PrintType.Normal: { return llDictionary.ToList(); } case PrintType.Ascend: { if (llDictionary.Count == 0) return null ; LinkedList<KeyValuePair<TKey, TValue>> newll = new LinkedList<KeyValuePair<TKey, TValue>>(); try { LinkedListNode<KeyValuePair<TKey, TValue>> lln = llDictionary.First; while (lln != null ) { LinkedListNode<KeyValuePair<TKey, TValue>> lln2 = newll.First; while (lln2 != null ) { //Key是字符串的情况 if (llDictionary.First.Value.Key.GetType() == "string" .GetType()) { if ( string .Compare(lln2.Value.Key.ToString(), lln.Value.Key.ToString()) > 0) { newll.AddBefore(lln2, new LinkedListNode <KeyValuePair<TKey, TValue>>(lln.Value)); break ; } } else //Key可以被转化为浮点数的情况 { if ( double .Parse(lln2.Value.Key.ToString()) > double .Parse(lln.Value.Key.ToString())) { newll.AddBefore(lln2, new LinkedListNode <KeyValuePair<TKey, TValue>>(lln.Value)); break ; } } lln2 = lln2.Next; } if (lln2 == null ) { newll.AddLast( new LinkedListNode <KeyValuePair<TKey, TValue>>(lln.Value)); } lln = lln.Next; } } catch (Exception ex) { Console.WriteLine(ex.Message); } return newll.ToList(); } case PrintType.Descend: { if (llDictionary.Count == 0) return null ; LinkedList<KeyValuePair<TKey, TValue>> newll = new LinkedList<KeyValuePair<TKey, TValue>>(); try { LinkedListNode<KeyValuePair<TKey, TValue>> lln = llDictionary.First; while (lln != null ) { LinkedListNode<KeyValuePair<TKey, TValue>> lln2 = newll.First; while (lln2 != null ) { //Key是字符串的情况 if (llDictionary.First.Value.Key.GetType() == "string" .GetType()) { if ( string .Compare(lln2.Value.Key.ToString(), lln.Value.Key.ToString()) < 0) { newll.AddBefore(lln2, new LinkedListNode <KeyValuePair<TKey, TValue>>(lln.Value)); break ; } } else //Key可以被转化为浮点数的情况 { if ( double .Parse(lln2.Value.Key.ToString()) < double .Parse(lln.Value.Key.ToString())) { newll.AddBefore(lln2, new LinkedListNode <KeyValuePair<TKey, TValue>>(lln.Value)); break ; } } lln2 = lln2.Next; } if (lln2 == null ) { newll.AddLast( new LinkedListNode <KeyValuePair<TKey, TValue>>(lln.Value)); } lln = lln.Next; } } catch (Exception ex) { Console.WriteLine(ex.Message); } return newll.ToList(); } default : return null ; } } } |
3.Main函数调用示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
static void Main( string [] args) { MyDictioinary< string , string > dic = new MyDictioinary< string , string >(); //插入两组数据 dic.Add( "1" , "MERCURY" ); dic.Add( "2" , "VENUS" ); //尝试插入重复主键的数据 报警:主键不存在 try { dic.Add( "1" , "EARTH" ); } catch (Exception ex) { Console.WriteLine(ex.Message); } //删除一组数据 dic.Delete( "1" ); //添加一组数据 dic.Add( "1" , "MARS" ); //修改两组数据 dic.Modify( "1" , "JUPITER" ); dic.Modify( "2" , "SATURN" ); //添加两组数据 dic.Add( "6" , "SUN" ); dic.Add( "5" , "MOON" ); dic.Add( "8" , "URANUS" ); dic.Add( "9" , "NEPTUNE" ); dic.PrintData(); //通过索引找到对应值 Console.WriteLine( "key为6的元素value为:" + dic[ "6" ]); Console.WriteLine( "key为3的元素value为:" + dic[ "3" ]); List<KeyValuePair< string , string >> l; //输出升序排列好的List l = dic.ToList(MyDictioinary< string , string >.PrintType.Ascend); Console.WriteLine( "升序排列后的List:" ); for ( int i = 0; i < l.Count; i++) { Console.WriteLine(l[i].Key.ToString() + "\t:" + l[i].Value.ToString()); } //输出降序排列好的List l = dic.ToList(MyDictioinary< string , string >.PrintType.Descend); Console.WriteLine( "降排列后的List:" ); for ( int i = 0; i < l.Count; i++) { Console.WriteLine(l[i].Key.ToString() + "\t:" + l[i].Value.ToString()); } Console.ReadLine(); } |
4.运行示例
希望本文所述对大家的C#程序设计有所帮助。