C#中用foreach語句遍歷數(shù)組及將數(shù)組作為參數(shù)的用法
對(duì)數(shù)組使用 foreach
C#提供 foreach 語句。 該語句提供一種簡(jiǎn)單、明了的方法來循環(huán)訪問數(shù)組或任何可枚舉集合的元素。 foreach 語句按數(shù)組或集合類型的枚舉器返回的順序處理元素,該順序通常是從第 0 個(gè)元素到最后一個(gè)元素。 例如,以下代碼創(chuàng)建一個(gè)名為 numbers 的數(shù)組,并使用 foreach 語句循環(huán)訪問該數(shù)組:
int[] numbers = { 4, 5, 6, 1, 2, 3, -2, -1, 0 }; foreach (int i in numbers) { System.Console.Write("{0} ", i); } // Output: 4 5 6 1 2 3 -2 -1 0
借助多維數(shù)組,你可以使用相同的方法來循環(huán)訪問元素,例如:
int[,] numbers2D = new int[3, 2] { { 9, 99 }, { 3, 33 }, { 5, 55 } }; // Or use the short form: // int[,] numbers2D = { { 9, 99 }, { 3, 33 }, { 5, 55 } }; foreach (int i in numbers2D) { System.Console.Write("{0} ", i); }
輸出:
9 99 3 33 5 55
但對(duì)于多維數(shù)組,使用嵌套的 for 循環(huán)可以更好地控制數(shù)組元素。
將數(shù)組作為參數(shù)傳遞
數(shù)組可作為實(shí)參傳遞給方法形參。由于數(shù)組是引用類型,因此方法可以更改元素的值。
將一維數(shù)組作為參數(shù)傳遞
可以將初始化的一維數(shù)組傳遞給方法。例如,下面的語句將數(shù)組發(fā)送到 print 方法。
int[] theArray = { 1, 3, 5, 7, 9 }; PrintArray(theArray);
下面的代碼顯示 print 方法的部分實(shí)現(xiàn)。
void PrintArray(int[] arr) { // Method code. }
您可以在一個(gè)步驟中初始化和傳遞新數(shù)組,如下面的示例所示。
PrintArray(new int[] { 1, 3, 5, 7, 9 });
示例
說明
在下面的示例中,將初始化一個(gè)字符串?dāng)?shù)組并將其作為參數(shù)傳遞到字符串的 PrintArray 方法。該方法顯示數(shù)組的元素。接下來,調(diào)用 ChangeArray 和 ChangeArrayElement 方法以演示通過值發(fā)送數(shù)組參數(shù)時(shí)不會(huì)阻止更改這些數(shù)組元素。
代碼
class ArrayClass { static void PrintArray(string[] arr) { for (int i = 0; i < arr.Length; i++) { System.Console.Write(arr[i] + "{0}", i < arr.Length - 1 ? " " : ""); } System.Console.WriteLine(); } static void ChangeArray(string[] arr) { // The following attempt to reverse the array does not persist when // the method returns, because arr is a value parameter. arr = (arr.Reverse()).ToArray(); // The following statement displays Sat as the first element in the array. System.Console.WriteLine("arr[0] is {0} in ChangeArray.", arr[0]); } static void ChangeArrayElements(string[] arr) { // The following assignments change the value of individual array // elements. arr[0] = "Sat"; arr[1] = "Fri"; arr[2] = "Thu"; // The following statement again displays Sat as the first element // in the array arr, inside the called method. System.Console.WriteLine("arr[0] is {0} in ChangeArrayElements.", arr[0]); } static void Main() { // Declare and initialize an array. string[] weekDays = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; // Pass the array as an argument to PrintArray. PrintArray(weekDays); // ChangeArray tries to change the array by assigning something new // to the array in the method. ChangeArray(weekDays); // Print the array again, to verify that it has not been changed. System.Console.WriteLine("Array weekDays after the call to ChangeArray:"); PrintArray(weekDays); System.Console.WriteLine(); // ChangeArrayElements assigns new values to individual array // elements. ChangeArrayElements(weekDays); // The changes to individual elements persist after the method returns. // Print the array, to verify that it has been changed. System.Console.WriteLine("Array weekDays after the call to ChangeArrayElements:"); PrintArray(weekDays); } }
輸出:
Sun Mon Tue Wed Thu Fri Sat arr[0] is Sat in ChangeArray. Array weekDays after the call to ChangeArray: Sun Mon Tue Wed Thu Fri Sat arr[0] is Sat in ChangeArrayElements. Array weekDays after the call to ChangeArrayElements: Sat Fri Thu Wed Thu Fri Sat
將多維數(shù)組作為參數(shù)傳遞
可采用與傳遞一維數(shù)組相同的方式將初始化的多維數(shù)組傳遞給方法。
int[,] theArray = { { 1, 2 }, { 2, 3 }, { 3, 4 } }; Print2DArray(theArray);
下面的代碼顯示 print 方法的部分聲明,該方法接受一個(gè)二維數(shù)組作為其參數(shù)。
void Print2DArray(int[,] arr) { // Method code. }
您可以在一個(gè)步驟中初始化和傳遞新數(shù)組,如下面的示例所示。
Print2DArray(new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } });
示例
說明
在下面的示例中,將初始化一個(gè)二維整數(shù)數(shù)組并將其傳遞到 Print2DArray 方法。該方法顯示數(shù)組的元素。
代碼
class ArrayClass2D { static void Print2DArray(int[,] arr) { // Display the array elements. for (int i = 0; i < arr.GetLength(0); i++) { for (int j = 0; j < arr.GetLength(1); j++) { System.Console.WriteLine("Element({0},{1})={2}", i, j, arr[i, j]); } } } static void Main() { // Pass the array as an argument. Print2DArray(new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } }); // Keep the console window open in debug mode. System.Console.WriteLine("Press any key to exit."); System.Console.ReadKey(); } }
輸出:
Element(0,0)=1 Element(0,1)=2 Element(1,0)=3 Element(1,1)=4 Element(2,0)=5 Element(2,1)=6 Element(3,0)=7 Element(3,1)=8
上一篇:AnyChat的視頻會(huì)議程序?qū)嵗斀?/a>
欄 目:C#教程
下一篇:使用C#實(shí)現(xiàn)讀取系統(tǒng)配置文件的代碼實(shí)例講解
本文標(biāo)題:C#中用foreach語句遍歷數(shù)組及將數(shù)組作為參數(shù)的用法
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6759.html
您可能感興趣的文章
- 01-10C#中實(shí)現(xiàn)一次執(zhí)行多條帶GO的sql語句實(shí)例
- 01-10C#編程自學(xué)之流程控制語句
- 01-10C#語句先后順序?qū)Τ绦虻慕Y(jié)果有影響嗎
- 01-10輕松學(xué)習(xí)C#的foreach迭代語句
- 01-10C#異常處理中try和catch語句及finally語句的用法示例
- 01-10C#使用foreach循環(huán)遍歷數(shù)組完整實(shí)例
- 01-10C#檢查foreach判讀是否為null的方法
- 01-10C#從foreach語句中枚舉元素看數(shù)組詳解
- 01-10淺談C#中的for循環(huán)與foreach循環(huán)
- 01-10深入理解C#中foreach遍歷的使用方法


閱讀排行
- 1C語言 while語句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
- 4C語言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10delphi制作wav文件的方法
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 04-02jquery與jsp,用jquery