向大家介紹C#數組操作,可能好多人還不了解C#數組操作,沒有關系,看完本文你肯定有不少收獲,希望本文能教會你更多東西。
數組是相同類型的對象的集合。由於數組幾乎可以為任意長度,因此可以使用數組存儲數千乃至數百萬個對象,但必須在創建數組時就確定其大小。數組中的每項都按索引進行訪問,索引是一個數字,指示對象在數組中的存儲位置或槽。數組既可用於存儲引用類型,也可用於存儲值類型。
C#數組操作程序:
1. using System;
2. using System.Collections.Generic;
3. using System.Text;
4.
5. namespace ClassAboutArray
6. {
7. public class CreateArray
8. {
9. /// <summary>
10. /// 一維數組的定義
11. /// </summary>
12. public void testArr1()
13. {
14. int[] myIntArr = new int[100];
15. //定義一個長度為100的int數組
16. string[] mystringArr = new string[100];
17. //定義一個長度為100的string數組
18. object[] myObjectArr = new object[100];
19. //定義一個長度為100的int數組
20.
21. int[] myIntArr2 = new int[] { 1, 2, 3 };
22. //定義一個int數組,長度為3
23. string[] mystringArr2 = new string[] { "油", "鹽" };
24. //定義一個string數組,長度為2
25. }
26.
27. /// <summary>
28. /// 多維數組的定義
29. /// </summary>
30. public void testArr2()
31. {
32. int[,] myIntArr = new int[10, 100];
33. //定義一個10*100的二維int數組
34. string[, ,] mystringArr = new string[2, 2, 3];
35. //定義一個2*2*3的三維string數組
36.
37. int[,] myIntArr2 = new int[,] { { 1, 2, 3 }, { -1, -2, -3 } };
38. //定義一個2*3的二維int數組,並初始化
39. string[,] mystringArr2 = new string[,] { { "油", "鹽" }, { "《圍城》", "《晨露》" } };
40. //定義一個2*2的二維string數組,並初始化
41. }
42.
43. /// <summary>
44. /// 交錯數組的定義
45. /// </summary>
46. public void testArr3()
47. {
48. int[][] myJaggedArray = new int[3][];
49. myJaggedArray[0] = new int[5];
50. myJaggedArray[1] = new int[4];
51. myJaggedArray[2] = new int[2];
52.
53. int[][] myJaggedArray2 = new int[][]
54. {
55. new int[] {1,3,5,7,9},
56. new int[] {0,2,4,6},
57. new int[] {11,22}
58. };
59. }
60. }
61.
62. public class TraverseArray
63. {
64. /// <summary>
65. /// 使用GetLowerBound|GetUpperBound遍歷數組
66. /// </summary>
67. public void test1()
68. {
69. //定義二維數組
70. string[,] myStrArr2 = new string[,]
{ { "油", "鹽" }, { "《圍城》", "《晨露》" }, { "毛毛熊", "Snoopy" } };
71. //循環輸出
72. for (int i = myStrArr2.GetLowerBound(0); i <= myStrArr2.GetUpperBound(0); i++)
73. {
74. Console.WriteLine("item{0}", i);
75. for (int j = myStrArr2.GetLowerBound(1); j <= myStrArr2.GetUpperBound(1); j++)
76. {
77. Console.WriteLine(" item{0}{1}:{2}", i, j, myStrArr2.GetValue(i, j));
78. }
79. }
80. }
81.
82. /// <summary>
83. /// 使用foreach遍歷數組
84. /// </summary>
85. public void test2()
86. {
87. //定義二維數組
88. string[,] myStrArr2 = new string[,]
{ { "油", "鹽" }, { "《圍城》", "《晨露》" }, { "毛毛熊", "Snoopy" } };
89. //循環輸出
90. foreach (string item in myStrArr2)
91. {
92. {
93. Console.WriteLine("{0}", item);
94. }
95. }
96. }
97. }
98.
99. public class SortArray
100. {
101. /// <summary>
102. /// 利用Sort方法進行數組排序
103. /// </summary>
104. public void test1()
105. {
106. //定義數組
107. int[] myArr = { 5, 4, 3, 2, 1 };
108.
109. //輸出原始數組:原始數組:5->4->3->2->1->
110. Console.WriteLine("原始數組:");
111. for (int i = 0; i < myArr.Length; i++)
112. Console.Write("{0}->", myArr[i]);
113. Console.WriteLine();
114.
115. //對數組排序
116. Array.Sort(myArr);
117.
118. //並輸出排序后的數組:1->2->3->4->5->
119. Console.WriteLine("排序以后數組:");
120. for (int i = 0; i < myArr.Length; i++)
121. Console.Write("{0}->", myArr[i]);
122. }
123.
124. /// <summary>
125. /// 多個數組的關鍵字排序
126. /// </summary>
127. public void test2()
128. {
129. //定義數組
130. int[] arrSid = { 5, 4, 3, 2, 1 };
131. string[] arrSname = { "張三", "李四", "王五", "麻子", "淘氣" };
132.
133. //輸出原始數組:原始數組:張三(5)->李四(4)->王五(3)->麻子(2)->淘氣(1)->
134. Console.WriteLine("原始數組:");
135. for (int i = 0; i < arrSid.Length; i++)
136. Console.Write("{0}({1})->", arrSname[i], arrSid[i]);
137. Console.WriteLine();
138.
139. //根據學號關鍵字排序
140. Array.Sort(arrSid, arrSname);
141.
142. //並輸出排序后的數組:淘氣(1)->麻子(2)->王五(3)->李四(4)->張三(5)
143. Console.WriteLine("排序以后數組:");
144. for (int i = 0; i < arrSid.Length; i++)
145. Console.Write("{0}({1})->", arrSname[i], arrSid[i]);
146. }
147. }
148.
149. public class SearchArray
150. {
151. /// <summary>
152. /// 利用BinarySearch方法搜索元素
153. /// </summary>
154. public void test1()
155. {
156. //定義數組
157. int[] myArr = { 5, 4, 3, 2, 1 };
158.
159. //對數組排序
160. Array.Sort(myArr);
161.
162. //搜索
163. int target = 3;
164. int result = Array.BinarySearch(myArr, target); //2
165. Console.WriteLine("{0}的下標為{1}", target, result); //2
166. }
167.
168. /// <summary>
169. /// 判斷是否包含某個值
170. /// </summary>
171. public void test2()
172. {
173. //定義數組
174. string[] arrSname = { "張三", "李四", "王五", "麻子", "淘氣" };
175.
176. //判斷是否含有某值
177. string target = "王五";
178. bool result = ((System.Collections.IList)arrSname).Contains(target);
179. Console.WriteLine("包含{0}?{1}", target, result); //true
180. }
181. }
182.
183. public class ReverseArray
184. {
185. /// <summary>
186. /// 利用Reverse方法反轉數組
187. /// </summary>
188. public void test1()
189. {
190. //定義數組
191. int[] myArr = { 5, 4, 3, 2, 1 };
192.
193. //輸出原始數組:原始數組:5->4->3->2->1->
194. Console.WriteLine("原始數組:");
195. for (int i = 0; i < myArr.Length; i++)
196. Console.Write("{0}->", myArr[i]);
197. Console.WriteLine();
198.
199. //對數組反轉
200. Array.Reverse(myArr);
201.
202. //並輸出反轉后的數組:1->2->3->4->5->
203. Console.WriteLine("反轉以后數組:");
204. for (int i = 0; i < myArr.Length; i++)
205. Console.Write("{0}->", myArr[i]);
206. }
207. }
208.
209. public class CopyArray
210. {
211. /// <summary>
212. /// 利用Copy靜態方法復制數組
213. /// </summary>
214. public void test1()
215. {
216. //定義數組
217. int[] myArr = { 5, 4, 3, 2, 1 };
218.
219. //輸出原始數組:原始數組:5->4->3->2->1->
220. Console.WriteLine("原始數組:");
221. for (int i = 0; i < myArr.Length; i++)
222. Console.Write("{0}->", myArr[i]);
223. Console.WriteLine();
224.
225. //復制數組
226. int[] newnewArr = new int[3];
227. Array.Copy(myArr, newArr, 3);
228.
229. //並輸出反復制的數組:5->4->3->
230. Console.WriteLine("復制數組:");
231. for (int i = 0; i < newArr.Length; i++)
232. Console.Write("{0}->", newArr[i]);
233. }
234.
235. /// <summary>
236. /// 利用CopyTo實例方法復制數組
237. /// </summary>
238. public void test2()
239. {
240. //定義數組
241. int[] myArr = { 5, 4, 3, 2, 1 };
242.
243. //輸出原始數組:原始數組:5->4->3->2->1->
244. Console.WriteLine("原始數組:");
245. for (int i = 0; i < myArr.Length; i++)
246. Console.Write("{0}->", myArr[i]);
247. Console.WriteLine();
248.
249. //復制數組
250. int[] newnewArr = new int[7];
251. myArr.CopyTo(newArr, 2);
252.
253. //並輸出反復制的數組:0->0->5->4->3->2->1->
254. Console.WriteLine("復制數組:");
255. for (int i = 0; i < newArr.Length; i++)
256. Console.Write("{0}->", newArr[i]);
257. }
258. }
259.
260. public class DynamicCreateArray
261. {
262. /// <summary>
263. /// 利用CreateInstance動態創建數組
264. /// </summary>
265. public void test1()
266. {
267. //定義長度數組
268. int[] lengthsArr = new int[] { 3, 4 };
269. int[] lowerBoundsArr = { 1, 11 };
270.
271. Array arr = Array.CreateInstance(Type.GetType("System.Int32"), lengthsArr, lowerBoundsArr);
272.
273. Random r = new Random(); //聲明一個隨機數對象
274. //循環賦值、輸出
275. for (int i = arr.GetLowerBound(0) - 1; i < arr.GetUpperBound(0) - 1; i++)
276. {
277. for (int j = arr.GetLowerBound(1) - 1; j < arr.GetUpperBound(1) - 1; j++)
278. {
279. arr.SetValue((int)r.Next() % 100, i, j);//用1~100的隨即數賦值
280. Console.WriteLine("arr[{0},{1}]={3}", i, j, arr.GetValue(i, j));
281. }
282. }
283. }
284. }
285. }