自定义泛型
泛型类,第一替代符T,第二替代符U
复制代码
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
79using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace m1w4d5_dictionary { class Program { static void Main(string[] args) { //把数字转换成中文字符 string inputStr = Console.ReadLine(); //123456789 //一二三四五 //字典<key,value> //声明变量 Dictionary<char, string> dic = new Dictionary<char, string>(); dic.Add('1', "一"); dic['2'] = "二"; dic['3'] = "三"; //修改一个元素 //dic.Add('1',"壹");位置已占,会报错 dic['2'] = "贰"; //访问元素 //Console.WriteLine(dic['2']); //遍历 foreach (var item in dic) { //Console.WriteLine(item); if (item.Key == '1') { Console.WriteLine(item.Value); } } Console.WriteLine(); Dictionary<char, char> dic1 = new Dictionary<char, char>(); string num = "1234567890"; string str = "一二三四五六七八九零"; for (int i = 0; i < num.Length; i++) { dic1[num[i]] = str[i]; } for (int i = 0; i < inputStr.Length; i++) { //如果用户输入的key不在我们的范围内,会报错 //通过ContainsKey判定字典中是否包含了对应的key if (dic1.ContainsKey(inputStr[i])) { Console.Write(dic1[inputStr[i]]); } } Console.WriteLine(); //计算字符串中每个字母出现的次数"Welcome to China! Welcome to HangKang!" //我能通过字母(char)找到他出现的次数(int) Dictionary<char, int> dic2 = new Dictionary<char, int>(); string str1 = "Welcome to China! Welcome to HangKang!"; for (int i = 0; i < str1.Length; i++) { //如果你在字典有了,字典中有了这个字母 if (dic2.ContainsKey(str1[i])) { //你的值就自增 dic2[str1[i]]++; } else { //你的值就是 dic2[str1[i]] = 1; } } foreach (var item in dic2) { Console.WriteLine(item.Key +":"+item.Value); } } } }
复制代码
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 泛型 { //定义 //泛型是C#的一种特性,可以让我们将一种结构(类,接口)或者一种逻辑(函数)应用到所有类型 //如果我们要把一个类型或者方法写成泛型的,我们只需要在名称后面加上<>,<>中可以填入若干个类型替代符 class MyList<T> { T[] data; public T this[int index] { get { if (index < 0 || index >= count) { throw new OutOfMemoryException(); } return data[index]; } set { if (index < 0 || index >= count) { throw new OutOfMemoryException(); } else { data[index] = value; } } } public int Capacity { get { return data.Length; } set { if (value == count) return; if (value < count) value = count; //建立新数组 T[] newData = new T[value]; //拷贝数据 for (int i = 0; i < data.Length; i++) newData[i] = data[i]; //用新数组换老数组 data = newData; } } int count = 0; public int Count => count; //添加 public MyList(int capacity = 4) { data = new T[capacity]; } //动态增长 public void Add(T item) { if (count >= Capacity) Capacity *= 2; data[count] = item; count++; } //删除 public void Remove(T item) { int index = IndexOf(item); if (index != -1) { RemoveAt(index); } } public void RemoveLast(T item) { int index = LastIndexOf(item); if (index != -1) { RemoveAt(index); } } public void RemoveAt(int index) { if (index >= count) { throw new IndexOutOfRangeException(); } for (int i = index + 1; i < count; i++) { data[i - 1] = data[i]; } count--; } public void RemoveAll(T item) { while (true) { int index = IndexOf(item); if (index != -1) { RemoveAt(index); } else { break; } } } //查找 public int IndexOf(T item) { int index = -1; for (int i = 0; i < count; i++) { if (item.Equals(data[i])) { return i; } } return index; } public int LastIndexOf(T item) { int index = -1; for (int i = count - 1; i >= 0; i--) { if (item.Equals(data[i])) { return i; } } return index; } public void Sort(Comparison<T> comparison) { for (int i = 0; i < count - 1; i++) { for (int j = 0; j < count - 1 - i; j++) { if (comparison(data[j], data[j + 1]) > 0) { T temp = data[j]; data[j] = data[j + 1]; data[j + 1] = temp; } } } } public void Sort(IComparer<T> iCompareer) { for (int i = 0; i < count - 1; i++) { for (int j = 0; j < count - 1 - i; j++) { if (iCompareer.Compare(data[j], data[j + 1]) > 0) { T temp = data[j]; data[j] = data[j + 1]; data[j + 1] = temp; } } } } public void Sort() { for (int i = 0; i < count - 1; i++) { for (int j = 0; j < count - 1 - i; j++) { IComparable<T> Icompar = data[j] as IComparable<T>; if (Icompar.CompareTo(data[j + 1]) > 0) { T temp = data[j]; data[j] = data[j + 1]; data[j + 1] = temp; } } } } } class Monster : IComparable<Monster> { public Monster(string name, int attack, int defend, int health) { this.name = name; this.attack = attack; this.defend = defend; this.health = health; } public string name; public int attack; public int defend; public int health; public override string ToString() { return string.Format("{0}:[attack:{1},defend:{2},health{3}]", name, attack, defend, health); } public int CompareTo(Monster other) { //比较某一个参数,返回对应值 //如果大就返回大于0的数 自已排在对比参数的后面 //如果小就返回小于0的数 自已排在对比参数的前面 //如果相等就返回0 不换 //这样在外部调用Sort的时候会 形成一个以这个参数为标准的升序排序 return attack - other.attack; } public static int AttackSort(Monster a, Monster b) { return a.attack - b.attack; } public static int DefendSort(Monster a, Monster b) { return a.defend - b.defend; } } class Program { static void Main(string[] args) { //List<int> list = new List<int>(); //list.Add(5); //list.Add(6); //list.Add(7); //list.Capacity = 0; //Console.WriteLine(list.Capacity); MyList<Monster> myList = new MyList<Monster>(); Random roll = new Random(); //Console.WriteLine(myList.Capacity); for (int i = 0; i < 10; i++) { myList.Add(new Monster(i.ToString(), roll.Next(5, 10), roll.Next(10, 15), roll.Next(100, 150))); } //myList[5] = 1; //myList[7] = 1; myList.Sort(); for (int i = 0; i < myList.Count; i++) { Console.WriteLine(myList[i]); } } } }
转载于:https://www.cnblogs.com/cnwuchao/p/10362559.html
最后
以上就是端庄小霸王最近收集整理的关于C#学习笔记(十九):字典的全部内容,更多相关C#学习笔记(十九)内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复