日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
C#數(shù)組操作詳細剖析

本文向大家介紹C#數(shù)組操作,可能好多人還不了解C#數(shù)組操作,沒有關(guān)系,看完本文你肯定有不少收獲,希望本文能教會你更多東西。

10年積累的網(wǎng)站設(shè)計、成都網(wǎng)站設(shè)計經(jīng)驗,可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認識你,你也不認識我。但先網(wǎng)站制作后付款的網(wǎng)站建設(shè)流程,更有平橋免費網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。

數(shù)組是相同類型的對象的集合。由于數(shù)組幾乎可以為任意長度,因此可以使用數(shù)組存儲數(shù)千乃至數(shù)百萬個對象,但必須在創(chuàng)建數(shù)組時就確定其大小。數(shù)組中的每項都按索引進行訪問,索引是一個數(shù)字,指示對象在數(shù)組中的存儲位置或槽。數(shù)組既可用于存儲 引用類型,也可用于存儲 值類型。

C#數(shù)組操作程序:

 
 
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4.  
  5. namespace ClassAboutArray  
  6. {  
  7. public class CreateArray  
  8. {  
  9. ///  
  10. /// 一維數(shù)組的定義  
  11. ///  
  12. public void testArr1()  
  13. {  
  14. int[] myIntArr = new int[100];  
  15. //定義一個長度為100的int數(shù)組  
  16. string[] mystringArr = new string[100];  
  17. //定義一個長度為100的string數(shù)組  
  18. object[] myObjectArr = new object[100];  
  19. //定義一個長度為100的int數(shù)組  
  20.  
  21. int[] myIntArr2 = new int[] { 1, 2, 3 };   
  22. //定義一個int數(shù)組,長度為3  
  23. string[] mystringArr2 = new string[] { "油", "鹽" };  
  24. //定義一個string數(shù)組,長度為2  
  25. }  
  26.  
  27. ///  
  28. /// 多維數(shù)組的定義  
  29. ///  
  30. public void testArr2()  
  31. {  
  32. int[,] myIntArr = new int[10, 100];   
  33. //定義一個10*100的二維int數(shù)組  
  34. string[, ,] mystringArr = new string[2, 2, 3];   
  35. //定義一個2*2*3的三維string數(shù)組   
  36.  
  37. int[,] myIntArr2 = new int[,] { { 1, 2, 3 }, { -1, -2, -3 } };  
  38. //定義一個2*3的二維int數(shù)組,并初始化  
  39. string[,] mystringArr2 = new string[,] { { "油", "鹽" }, { "《圍城》", "《晨露》" } };  
  40. //定義一個2*2的二維string數(shù)組,并初始化  
  41. }  
  42.  
  43. ///  
  44. /// 交錯數(shù)組的定義  
  45. ///  
  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. ///  
  65. /// 使用GetLowerBound|GetUpperBound遍歷數(shù)組  
  66. ///  
  67. public void test1()  
  68. {  
  69. //定義二維數(shù)組  
  70. string[,] myStrArr2 = new string[,] 
    { { "油", "鹽" }, { "《圍城》", "《晨露》" }, { "毛毛熊", "Snoopy" } };  
  71. //循環(huán)輸出  
  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. ///  
  83. /// 使用foreach遍歷數(shù)組  
  84. ///  
  85. public void test2()  
  86. {  
  87. //定義二維數(shù)組  
  88. string[,] myStrArr2 = new string[,] 
    { { "油", "鹽" }, { "《圍城》", "《晨露》" }, { "毛毛熊", "Snoopy" } };  
  89. //循環(huán)輸出  
  90. foreach (string item in myStrArr2)  
  91. {  
  92. {  
  93. Console.WriteLine("{0}", item);  
  94. }  
  95. }  
  96. }  
  97. }  
  98.  
  99. public class SortArray  
  100. {  
  101. ///  
  102. /// 利用Sort方法進行數(shù)組排序  
  103. ///  
  104. public void test1()  
  105. {  
  106. //定義數(shù)組  
  107. int[] myArr = { 5, 4, 3, 2, 1 };  
  108.  
  109. //輸出原始數(shù)組:原始數(shù)組:5->4->3->2->1-> 
  110. Console.WriteLine("原始數(shù)組:");  
  111. for (int i = 0; i < myArr.Length; i++)  
  112. Console.Write("{0}->", myArr[i]);  
  113. Console.WriteLine();  
  114.  
  115. //對數(shù)組排序  
  116. Array.Sort(myArr);  
  117.  
  118. //并輸出排序后的數(shù)組:1->2->3->4->5-> 
  119. Console.WriteLine("排序以后數(shù)組:");  
  120. for (int i = 0; i < myArr.Length; i++)  
  121. Console.Write("{0}->", myArr[i]);  
  122. }  
  123.  
  124. ///  
  125. /// 多個數(shù)組的關(guān)鍵字排序  
  126. ///  
  127. public void test2()  
  128. {  
  129. //定義數(shù)組  
  130. int[] arrSid = { 5, 4, 3, 2, 1 };  
  131. string[] arrSname = { "張三", "李四", "王五", "麻子", "淘氣" };  
  132.  
  133. //輸出原始數(shù)組:原始數(shù)組:張三(5)->李四(4)->王五(3)->麻子(2)->淘氣(1)-> 
  134. Console.WriteLine("原始數(shù)組:");  
  135. for (int i = 0; i < arrSid.Length; i++)  
  136. Console.Write("{0}({1})->", arrSname[i], arrSid[i]);  
  137. Console.WriteLine();  
  138.  
  139. //根據(jù)學號關(guān)鍵字排序  
  140. Array.Sort(arrSid, arrSname);  
  141.  
  142. //并輸出排序后的數(shù)組:淘氣(1)->麻子(2)->王五(3)->李四(4)->張三(5)  
  143. Console.WriteLine("排序以后數(shù)組:");  
  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. ///  
  152. /// 利用BinarySearch方法搜索元素  
  153. ///  
  154. public void test1()  
  155. {  
  156. //定義數(shù)組  
  157. int[] myArr = { 5, 4, 3, 2, 1 };  
  158.  
  159. //對數(shù)組排序  
  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. ///  
  169. /// 判斷是否包含某個值  
  170. ///  
  171. public void test2()  
  172. {  
  173. //定義數(shù)組  
  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. ///  
  186. /// 利用Reverse方法反轉(zhuǎn)數(shù)組  
  187. ///  
  188. public void test1()  
  189. {  
  190. //定義數(shù)組  
  191. int[] myArr = { 5, 4, 3, 2, 1 };  
  192.  
  193. //輸出原始數(shù)組:原始數(shù)組:5->4->3->2->1-> 
  194. Console.WriteLine("原始數(shù)組:");  
  195. for (int i = 0; i < myArr.Length; i++)  
  196. Console.Write("{0}->", myArr[i]);  
  197. Console.WriteLine();  
  198.  
  199. //對數(shù)組反轉(zhuǎn)  
  200. Array.Reverse(myArr);  
  201.  
  202. //并輸出反轉(zhuǎn)后的數(shù)組:1->2->3->4->5-> 
  203. Console.WriteLine("反轉(zhuǎn)以后數(shù)組:");  
  204. for (int i = 0; i < myArr.Length; i++)  
  205. Console.Write("{0}->", myArr[i]);  
  206. }  
  207. }  
  208.  
  209. public class CopyArray  
  210. {  
  211. ///  
  212. /// 利用Copy靜態(tài)方法復制數(shù)組  
  213. ///  
  214. public void test1()  
  215. {  
  216. //定義數(shù)組  
  217. int[] myArr = { 5, 4, 3, 2, 1 };  
  218.  
  219. //輸出原始數(shù)組:原始數(shù)組:5->4->3->2->1-> 
  220. Console.WriteLine("原始數(shù)組:");  
  221. for (int i = 0; i < myArr.Length; i++)  
  222. Console.Write("{0}->", myArr[i]);  
  223. Console.WriteLine();  
  224.  
  225. //復制數(shù)組  
  226. int[] newnewArr = new int[3];  
  227. Array.Copy(myArr, newArr, 3);  
  228.  
  229. //并輸出反復制的數(shù)組:5->4->3-> 
  230. Console.WriteLine("復制數(shù)組:");  
  231. for (int i = 0; i < newArr.Length; i++)  
  232. Console.Write("{0}->", newArr[i]);  
  233. }  
  234.  
  235. ///  
  236. /// 利用CopyTo實例方法復制數(shù)組  
  237. ///  
  238. public void test2()  
  239. {  
  240. //定義數(shù)組  
  241. int[] myArr = { 5, 4, 3, 2, 1 };  
  242.  
  243. //輸出原始數(shù)組:原始數(shù)組:5->4->3->2->1-> 
  244. Console.WriteLine("原始數(shù)組:");  
  245. for (int i = 0; i < myArr.Length; i++)  
  246. Console.Write("{0}->", myArr[i]);  
  247. Console.WriteLine();  
  248.  
  249. //復制數(shù)組  
  250. int[] newnewArr = new int[7];  
  251. myArr.CopyTo(newArr, 2);  
  252.  
  253. //并輸出反復制的數(shù)組:0->0->5->4->3->2->1-> 
  254. Console.WriteLine("復制數(shù)組:");  
  255. for (int i = 0; i < newArr.Length; i++)  
  256. Console.Write("{0}->", newArr[i]);  
  257. }  
  258. }  
  259.  
  260. public class DynamicCreateArray  
  261. {  
  262. ///  
  263. /// 利用CreateInstance動態(tài)創(chuàng)建數(shù)組  
  264. ///  
  265. public void test1()  
  266. {  
  267. //定義長度數(shù)組  
  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(); //聲明一個隨機數(shù)對象  
  274. //循環(huán)賦值、輸出  
  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的隨即數(shù)賦值  
  280. Console.WriteLine("arr[{0},{1}]={3}", i, j, arr.GetValue(i, j));  
  281. }  
  282. }  
  283. }  
  284. }  

網(wǎng)頁題目:C#數(shù)組操作詳細剖析
轉(zhuǎn)載來于:http://www.dlmjj.cn/article/dpsjchh.html