C#頂用foreach語句遍歷數組及將數組作為參數的用法。本站提示廣大學習愛好者:(C#頂用foreach語句遍歷數組及將數組作為參數的用法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#頂用foreach語句遍歷數組及將數組作為參數的用法正文
對數組應用 foreach
C#供給 foreach 語句。 該語句供給一種簡略、清楚明了的辦法來輪回拜訪數組或任何可列舉聚集的元素。 foreach 語句按數組或聚集類型的列舉器前往的次序處置元素,該次序平日是從第 0 個元素到最初一個元素。 例如,以下代碼創立一個名為 numbers 的數組,並應用 foreach 語句輪回拜訪該數組:
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
借助多維數組,你可使用雷同的辦法來輪回拜訪元素,例如:
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
但關於多維數組,應用嵌套的 for 輪回可以更好地掌握數組元素。
將數組作為參數傳遞
數組可作為實參傳遞給辦法形參。因為數組是援用類型,是以辦法可以更改元素的值。
將一維數組作為參數傳遞
可以將初始化的一維數組傳遞給辦法。例如,上面的語句將數組發送到 print 辦法。
int[] theArray = { 1, 3, 5, 7, 9 }; PrintArray(theArray);
上面的代碼顯示 print 辦法的部門完成。
void PrintArray(int[] arr) { // Method code. }
您可以在一個步調中初始化和傳遞新數組,以下面的示例所示。
PrintArray(new int[] { 1, 3, 5, 7, 9 });
示例
解釋
鄙人面的示例中,將初始化一個字符串數組並將其作為參數傳遞到字符串的 PrintArray 辦法。該辦法顯示數組的元素。接上去,挪用 ChangeArray 和 ChangeArrayElement 辦法以演示經由過程值發送數組參數時不會阻攔更改這些數組元素。
代碼
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
將多維數組作為參數傳遞
可采取與傳遞一維數組雷同的方法將初始化的多維數組傳遞給辦法。
int[,] theArray = { { 1, 2 }, { 2, 3 }, { 3, 4 } }; Print2DArray(theArray);
上面的代碼顯示 print 辦法的部門聲明,該辦法接收一個二維數組作為其參數。
void Print2DArray(int[,] arr) { // Method code. }
您可以在一個步調中初始化和傳遞新數組,以下面的示例所示。
Print2DArray(new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } });
示例
解釋
鄙人面的示例中,將初始化一個二維整數數組並將其傳遞到 Print2DArray 辦法。該辦法顯示數組的元素。
代碼
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