-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollection_List.cs
223 lines (179 loc) · 5.76 KB
/
Collection_List.cs
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
public class MyIntList {
int[] array;
int capacity;
int count;
public int Count => count;
// 索引器
public int this[int index] {
get {
return array[index];
}
set {
array[index] = value;
}
}
public int GetValue(int index) {
return array[index];
}
public void SetValue(int index, int value) {
array[index] = value;
}
// public int Count {
// get {
// return count;
// }
// }
public int GetCount() {
return count;
}
public void SetCount(int value) {
count = value;
}
public MyIntList(int capacity = 4) {
this.capacity = capacity;
this.count = 0;
this.array = new int[capacity];
}
public void Add(int value) {
array[count] = value;
count += 1;
// 扩容
if (count == capacity) {
capacity *= 2;
// 拷贝
int[] newArray = new int[capacity];
for (int i = 0; i < count; i += 1) {
newArray[i] = array[i];
}
array = newArray;
}
}
public void Remove(int value) {
// Plan A: C# 官方做法 O(n^2)
// value == 1;
// array: 8 [1] 2 3 5 4
// index: 0 1 2 3 4 5
// array: 8 2 3 5 4
// index: 0 1 2 3 4
// for (int i = 0; i < count; i += 1) {
// if (array[i] == value) {
// for (int j = i; j < count - 1; j += 1) {
// array[j] = array[j + 1];
// }
// count -= 1;
// break;
// }
// }
// Plan B: 我的做法 O(n)
// value == 1;
// array: 8 [1] 2 3 5 4
// index: 0 1 2 3 4 5
// array: 8 4 2 3 5
// index: 0 1 2 3 4
for (int i = 0; i < count; i += 1) {
if (array[i] == value) {
array[i] = array[count - 1];
count -= 1;
}
}
}
}
public static class Collection_List_App {
public static void Entry() {
// 数据结构的作用:
// 1. 存储(添加, 删除, 查找, 遍历) CURD
// 2. 优缺点:
// - 添加快, 遍历快, 查找慢: List
// - 添加慢, 遍历快, 查找快: SortedList
// - 添加快, 遍历慢, 查找快: Dictionary
// - 添加快, 遍历快, 查找快: SortedDictionary 但消耗内存
Queue<int> queue = new Queue<int>();
Stack<int> stack = new Stack<int>();
Dictionary<int, object> dict = new Dictionary<int, object>();
SortedDictionary<int, object> sdict = new SortedDictionary<int, object>();
HashSet<int> hashset = new HashSet<int>();
SortedList<int, object> slist = new SortedList<int, object>();
LinkedList<int> linkedlist = new LinkedList<int>();
// WhatList();
WhatSortedList();
}
// 1. 什么是List
static void WhatList() {
// 作用:
// 1. 当不知道要存多少数据时
List<int> list = new List<int>(4);
for (int i = 0; i < 100; i += 1) {
list.Add(i);
}
for (int i = 0; i < list.Count; i += 1) {
System.Console.WriteLine($"List: {list[i]}");
}
// 原理:
// 1. List是一个动态数组, 内部是一个数组
MyIntList myList = new MyIntList();
for (int i = 0; i < 100; i += 1) {
myList.Add(i);
}
for (int i = 0; i < myList.Count; i += 1) {
// System.Console.WriteLine($"MyList: {myList.GetValue(i)}");
System.Console.WriteLine($"MyList: {myList[i]}");
}
}
// 2.
static void WhatSortedList() {
Stopwatch sw = new Stopwatch();
Random rd = new Random(38);
// ==== LIST ====
List<int> list = new List<int>(20_0000);
// SortedList 禁止重复 Key
SortedList<int, int> slist = new SortedList<int, int>(20_0000);
// Dictionary 禁止重复 Key
// 添加和查找依赖于 Key
Dictionary<int, int> dict = new Dictionary<int, int>(20_0000);
// 添加
for (int i = 20_0000 - 1; i >= 0; i -= 1) {
// int value = rd.Next(0, 100);
list.Add(i);
slist.Add(i, i);
dict.Add(i, i);
}
// 手动排序
// list.Sort();
// list.Sort((a, b) => {
// int smaller = a.CompareTo(b); // 如果 a < b 返回 -1; 如果 a == b 返回 0; 如果 a > b 返回 1;
// return smaller;
// });
// 显示
// for (int i = 0; i < list.Count; i += 1) {
// System.Console.WriteLine($"List: {list[i]}");
// }
// ==== SortedList ====
// 会以Key作为排序依据
// 从小到大排
// for (int i = 0; i < slist.Count; i += 1) {
// System.Console.WriteLine($"SortedList: {slist.Values[i]}");
// }
// ==== 查找 ====
{
sw.Restart();
int res = slist.IndexOfKey(199999);
sw.Stop();
System.Console.WriteLine($"SortedList IndexOfKey: {res}, {sw.Elapsed.TotalMicroseconds}mrs");
}
{
sw.Restart();
bool has = dict.TryGetValue(199999, out int res);
sw.Stop();
System.Console.WriteLine($"Dictionary: {res}, {sw.Elapsed.TotalMicroseconds}mrs");
}
{
sw.Restart();
int res = list.Find(value => value == 199999);
sw.Stop();
System.Console.WriteLine($"List FindIndex: {res}, {sw.Elapsed.TotalMicroseconds}mrs");
}
}
}