<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # C# | 數組 > 原文: [https://www.geeksforgeeks.org/c-sharp-arrays/](https://www.geeksforgeeks.org/c-sharp-arrays/) 數組是一組由普通名稱引用的相似類型的變量。 每個數據項都稱為數組的元素。 元素的數據類型可以是任何有效的數據類型,例如`char`,`int`,`float`等,并且元素存儲在連續的位置。 **數組的長度**指定數組中存在的元素數。 在 [**C#**](https://www.geeksforgeeks.org/introduction-to-c-sharp/) 中,數組的內存分配是動態完成的。 而且數組是一種對象,因此使用預定義的函數很容易找到它們的大小。 數組中的變量是有序的,每個變量的索引都從 0 開始。C# 中的數組工作與 C/C++ 中的不同。 有關 C# 中數組的重要注意事項 * 在 C# 中,所有數組都是動態分配的。 * 由于數組是 C# 中的對象,因此我們可以使用成員長度來找到它們的長度。 這與 C/C++ 不同,在 C/C++ 中,我們使用`sizeof`運算符查找長度。 * 像其他變量一樣,也可以在數據類型之后使用`[]`聲明 C# 數組變量。 * 數組中的變量是有序的,每個變量的索引都從 0 開始。 * C# 數組是基本類型為`System.Array`的對象。 * 數字數組和引用類型元素的默認值分別設置為零和`null`。 * 鋸齒狀數組元素是引用類型,并且被初始化為`null`。 * 數組元素可以是任何類型,包括數組類型。 * 數組類型是從抽象基本類型`Array`派生的引用類型。 這些類型實現`IEnumerable`,為此,它們在 C# 中的所有數組上使用`foreach`迭代。 根據數組的定義,數組可以包含基本數據類型以及類的對象。 每當使用基元數據類型時,實際值都必須存儲在連續的內存位置中。 對于類的對象,實際對象存儲在堆段中。 下圖顯示了數組如何順序存儲值: [![C# Arrays](https://img.kancloud.cn/93/60/9360052ebbb5d183e5b0ece8cbd9b4ad_875x410.png)](https://media.geeksforgeeks.org/wp-content/uploads/C-Arrays.jpg) **說明**: 索引從 0 開始,存儲值。 我們還可以在數組中存儲固定數量的值。 每當數組索引未達到數組大小時,將按順序將其增加 1。 數組聲明 **語法**: ``` < Data Type > [ ] < Name_Array > ``` **此處,** <數據類型>:它定義數組的元素類型。 []:它定義數組的大小。 < Name_Array >:這是數組的名稱。 **示例**: ``` int[] x; // can store int values string[] s; // can store string values double[] d; // can store double values Student[] stud1; // can store instances of Student class which is custom class ``` **注意**:僅聲明數組不會為該數組分配內存。 對于該數組,必須初始化。 數組初始化 如前所述,數組是引用類型,因此**新的**關鍵字用于創建數組的實例。 我們可以在索引的幫助下分配初始化的單個數組元素。 **Syntax :** ``` type [ ] < Name_Array > = new < datatype > [size]; ``` 在這里,`type`指定要分配的數據的類型,`size`指定數組中元素的數量,`Name_Array`是數組變量的名稱。 并且`new`將根據其大小為數組分配內存。 **示例:展示數組聲明和初始化的不同方法** **示例 1**: ``` // defining array with size 5\. // But not assigns values int[] intArray1 = new int[5]; ``` 上面的語句聲明&初始化可以存儲五個`int`值的`int`類型數組。 數組大小在方括號(`[]`)中指定。 **示例 2**: ``` // defining array with size 5 and assigning // values at the same time int[] intArray2 = new int[5]{1, 2, 3, 4, 5}; ``` 上面的語句與之相同,但是它為{}中的每個索引分配值。 **示例 3**: ``` // defining array with 5 elements which // indicates the size of an array int[] intArray3 = {1, 2, 3, 4, 5}; ``` 在上面的語句中,數組的值直接初始化而無需占用其大小。 因此,數組大小將自動為直接獲取的值的數量。 聲明后初始化數組 聲明后可以初始化數組。 不必使用`new`關鍵字同時聲明和初始化。 但是,在聲明之后初始化數組,必須使用`new`關鍵字對其進行初始化。 只能通過分配值來初始化它。 **Example :** ``` // Declaration of the array string[] str1, str2; // Initialization of array str1 = new string[5]{ “Element 1”, “Element 2”, “Element 3”, “Element 4”, “Element 5” }; str2 = new string[5]{ “Element 1”, “Element 2”, “Element 3”, “Element 4”, “Element 5” }; ``` **注意**:沒有給出大小的初始化在 C# 中無效。 它將給出一個編譯時錯誤。 **示例:初始化數組**的聲明錯誤 ``` // compile-time error: must give size of an array int[] intArray = new int[]; // error : wrong initialization of an array string[] str1; str1 = {“Element 1”, “Element 2”, “Element 3”, “Element 4” }; ``` 訪問數組元素 在初始化時,我們可以分配值。 但是,我們也可以在聲明和初始化之后使用其索引隨機分配數組的值。 我們可以通過索引來訪問數組值,將元素的索引放置在方括號內,數組名稱為 **示例**: ``` //declares & initializes int type array int[] intArray = new int[5]; // assign the value 10 in array on its index 0 intArray[0] = 10; // assign the value 30 in array on its index 2 intArray[2] = 30; // assign the value 20 in array on its index 1 intArray[1] = 20; // assign the value 50 in array on its index 4 intArray[4] = 50; // assign the value 40 in array on its index 3 intArray[3] = 40; // Accessing array elements using index intArray[0]; //returns 10 intArray[2]; //returns 30 ``` **實現**: **使用不同的循環訪問數組元素** ``` // C# program to illustrate creating an array // of integers, puts some values in the array, // and prints each value to standard output. using System; namespace geeksforgeeks { class GFG { ????// Main Method ????public static void Main() ????{ ????????// declares an Array of integers. ????????int[] intArray; ????????// allocating memory for 5 integers. ????????intArray = new int[5]; ????????// initialize the first elements ????????// of the array ????????intArray[0] = 10; ????????// initialize the second elements ????????// of the array ????????intArray[1] = 20; ????????// so on... ????????intArray[2] = 30; ????????intArray[3] = 40; ????????intArray[4] = 50; ????????// accessing the elements ????????// using for loop ????????Console.Write("For loop :"); ????????for (int i = 0; i < intArray.Length; i++) ????????????Console.Write(" " + intArray[i]); ????????Console.WriteLine(""); ????????Console.Write("For-each loop :"); ????????// using for-each loop ????????foreach(int i in intArray) ????????????Console.Write(" " + i); ????????Console.WriteLine(""); ????????Console.Write("while loop :"); ????????// using while loop ????????int j = 0; ????????while (j < intArray.Length) { ????????????Console.Write(" " + intArray[j]); ????????????j++; ????????} ????????Console.WriteLine(""); ????????Console.Write("Do-while loop :"); ????????// using do-while loop ????????int k = 0; ????????do ????????{ ????????????Console.Write(" " + intArray[k]); ????????????k++; ????????} while (k < intArray.Length); ????} } } ``` **輸出**: ``` For loop : 10 20 30 40 50 For-each loop : 10 20 30 40 50 while loop : 10 20 30 40 50 Do-while loop : 10 20 30 40 50 ``` 一維數組 在此數組中僅包含一行用于存儲值。 該數組的所有值從 0 到數組大小連續存儲。 例如,聲明 5 個整數的一維數組: ``` int[] arrayint = new int[5]; ``` 上面的數組包含從`arrayint[0]`到`arrayint[4]`的元素。 在這里,`new`運算符必須創建數組并通過其默認值初始化其元素。 上面的示例中,所有元素都由零初始化,因為它是`int`類型。 **Example :** ``` // C# program to creating an array // of the string as week days, store? // day values in the weekdays, // and prints each value. using System; namespace geeksforgeeks { class GFG { ????// Main Method ????public static void Main() ????{ ????????// declares a 1D Array of string. ????????string[] weekDays; ????????// allocating memory for days. ????????weekDays = new string[] {"Sun", "Mon", "Tue", "Wed",? ???????????????????????????????????????"Thu", "Fri", "Sat"}; ????????// Displaying Elements of array ????????foreach(string day in weekDays) ????????????Console.Write(day + " "); ????} } } ``` **輸出**: ``` Sun Mon Tue Wed Thu Fri Sat ``` 多維數組 多維數組包含多個行來存儲值。 在 [C#](https://www.geeksforgeeks.org/introduction-to-c-sharp/)中也稱為**矩形數組**,因為它的每行長度都是相同的。 它可以是 **2D 數組**或 **3D 數組**或更多。 為了存儲和訪問數組的值,需要嵌套循環。 多維數組的聲明,初始化和訪問如下: ``` // creates a two-dimensional array of // four rows and two columns. int[, ] intarray = new int[4, 2]; //creates an array of three dimensions, 4, 2, and 3 int[,, ] intarray1 = new int[4, 2, 3]; ``` **Example :** ``` // C# program to illustrate creating // an multi- dimensional array // puts some values in the array, // and print them using System; namespace geeksforgeeks { class GFG { ????// Main Method ????public static void Main() ????{ ????????// Two-dimensional array ????????int[, ] intarray = new int[, ] { { 1, 2 }, ?????????????????????????????????????????{ 3, 4 },? ?????????????????????????????????????????{ 5, 6 },? ?????????????????????????????????????????{ 7, 8 } }; ????????// The same array with dimensions? ????????// specified 4 row and 2 column. ????????int[, ] intarray_d = new int[4, 2] { { 1, 2 }, { 3, 4 },? ?????????????????????????????????????????????{ 5, 6 }, { 7, 8 } }; ????????// A similar array with string elements. ????????string[, ] str = new string[4, 2] { { "one", "two" },? ????????????????????????????????????????????{ "three", "four" },? ????????????????????????????????????????????{ "five", "six" },? ????????????????????????????????????????????{ "seven", "eight" } }; ????????// Three-dimensional array. ????????int[,, ] intarray3D = new int[,, ] { { { 1, 2, 3 },? ?????????????????????????????????????????????{ 4, 5, 6 } }, ?????????????????????????????????????????????{ { 7, 8, 9 },? ???????????????????????????????????????????{ 10, 11, 12 } } }; ????????// The same array with dimensions? ????????// specified 2, 2 and 3\. ????????int[,, ] intarray3Dd = new int[2, 2, 3] { { { 1, 2, 3 },? ??????????????????????????????????????????????????{ 4, 5, 6 } },? ??????????????????????????????????????????????????{ { 7, 8, 9 },? ????????????????????????????????????????????????{ 10, 11, 12 } } }; ????????// Accessing array elements. ????????Console.WriteLine("2DArray[0][0] : " + intarray[0, 0]); ????????Console.WriteLine("2DArray[0][1] : " + intarray[0, 1]); ????????Console.WriteLine("2DArray[1][1] : " + intarray[1, 1]); ????????Console.WriteLine("2DArray[2][0] " + intarray[2, 0]); ????????Console.WriteLine("2DArray[1][1] (other) : "? ?????????????????????????????????+ intarray_d[1, 1]); ????????Console.WriteLine("2DArray[1][0] (other)"? ?????????????????????????????+ intarray_d[1, 0]); ????????Console.WriteLine("3DArray[1][0][1] : "? ???????????????????????????+ intarray3D[1, 0, 1]); ????????Console.WriteLine("3DArray[1][1][2] : "? ??????????????????????????+ intarray3D[1, 1, 2]); ????????Console.WriteLine("3DArray[0][1][1] (other): "? ?????????????????????????????+ intarray3Dd[0, 1, 1]); ????????Console.WriteLine("3DArray[1][0][2] (other): "? ?????????????????????????????+ intarray3Dd[1, 0, 2]); ????????// using nested loop show string elements ????????Console.WriteLine("To String element"); ????????for (int i = 0; i < 4; i++) ????????????for (int j = 0; j < 2; j++) ????????????????Console.Write(str[i, j] + " "); ????} } } ``` **輸出**: ``` 2DArray[0][0] : 1 2DArray[0][1] : 2 2DArray[1][1] : 4 2DArray[2][0] 5 2DArray[1][1] (other) : 4 2DArray[1][0] (other)3 3DArray[1][0][1] : 8 3DArray[1][1][2] : 12 3DArray[0][1][1] (other): 5 3DArray[1][0][2] (other): 9 To String element one two three four five six seven eight ``` 鋸齒狀數組 元素為數組的數組稱為鋸齒數組,其含義是“ **數組**”。 鋸齒狀的數組元件可以具有不同的尺寸和大小。 以下示例顯示了如何聲明,初始化和訪問鋸齒狀數組。 **Example :** ``` // C# program to single-dimensional jagged array // that contains two single-dimensional array // elements of different sizes. using System; namespace geeksforgeeks { class GFG { ????// Main Method ????public static void Main() ????{ ????????/*----------2D Array---------------*/ ????????// Declare the array of two elements: ????????int[][] arr = new int[2][]; ????????// Initialize the elements: ????????arr[0] = new int[5] { 1, 3, 5, 7, 9 }; ????????arr[1] = new int[4] { 2, 4, 6, 8 }; ????????// Another way of Declare and ????????// Initialize of elements ????????int[][] arr1 = { new int[] { 1, 3, 5, 7, 9 }, ?????????????????????????new int[] { 2, 4, 6, 8 } }; ????????// Display the array elements: ????????for (int i = 0; i < arr.Length; i++) ????????{ ????????????System.Console.Write("Element [" + i + "] Array: "); ????????????for (int j = 0; j < arr[i].Length; j++) ????????????????Console.Write(arr[i][j] + " "); ????????????Console.WriteLine(); ????????} ????????Console.WriteLine("Another Array"); ????????// Display the another array elements: ????????for (int i = 0; i < arr1.Length; i++)? ????????{ ????????????System.Console.Write("Element [" + i + "] Array: "); ????????????for (int j = 0; j < arr1[i].Length; j++) ????????????????Console.Write(arr1[i][j] + " "); ????????????Console.WriteLine(); ????????} ????} } } ``` **輸出**: ``` Element [0] Array: 1 3 5 7 9 Element [1] Array: 2 4 6 8 Another Array Element [0] Array: 1 3 5 7 9 Element [1] Array: 2 4 6 8 ``` 可以混合鋸齒狀和多維數組。 鋸齒狀數組是數組的數組,因此其元素是引用類型,并且被初始化為`null`。 **示例**:聲明和初始化一維鋸齒狀數組,該數組包含三個大小不同的二維數組元素。 ``` // C# program to single-dimensional jagged array // that contains three two-dimensional array // elements of different sizes. using System; namespace geeksforgeeks { class GFG { // Main Method public static void Main() { ????int[][, ] arr = new int[3][, ] {new int[, ] {{1, 3}, {5, 7}}, ????????????????????????????????????new int[, ] {{0, 2}, {4, 6}, {8, 10}}, ????????????????????????????????????new int[, ] {{11, 22}, {99, 88}, {0, 9}}}; ????// Display the array elements: ????for (int i = 0; i < arr.Length; i++) ????{ ????????int x = 0; ????????for (int j = 0; j < arr[i].GetLength(x); j++)? ????????{ ????????????for (int k = 0; k < arr[j].Rank; k++) ????????????????Console.Write(" arr[" + i + "][" + j + ", " + k + "]:"? ???????????????????????????????????????????????+ arr[i][j, k] + " "); ????????????Console.WriteLine(); ????????} ????????x++; ????????Console.WriteLine(); ????} } } } ``` **輸出**: ``` arr[0][0, 0]:1 arr[0][0, 1]:3 arr[0][1, 0]:5 arr[0][1, 1]:7 arr[1][0, 0]:0 arr[1][0, 1]:2 arr[1][1, 0]:4 arr[1][1, 1]:6 arr[1][2, 0]:8 arr[1][2, 1]:10 arr[2][0, 0]:11 arr[2][0, 1]:22 arr[2][1, 0]:99 arr[2][1, 1]:88 arr[2][2, 0]:0 arr[2][2, 1]:9 ``` **要記住的要點**: * `GetLength(int)`:返回數組第一維中的元素數。 * 當使用鋸齒狀數組時,如果索引不存在則是安全的,那么它將拋出`IndexOutOfRange`異常。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看