<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之旅 廣告
                {% raw %} # C# 數組 > 原文: [https://zetcode.com/lang/csharp/arrays/](https://zetcode.com/lang/csharp/arrays/) 在 C# 編程教程的這一部分中,我們將介紹數組。 我們將初始化數組并從中讀取數據。 ## C# 數組定義 數組是數據的集合。 標量變量一次只能容納一項。 數組可以容納多個項目。 這些項目稱為數組的元素。 數組存儲相同數據類型的數據。 每個元素都可以由索引引用。 數組從零開始。 第一個元素的索引為零。 數組是引用類型。 數組用于存儲我們應用的數據。 我們聲明數組為某種數據類型。 我們指定它們的長度。 我們用數據初始化數組。 我們有幾種使用數組的方法。 我們可以修改元素,對其進行排序,復制或搜索。 ```cs int[] ages; String[] names; float[] weights; ``` 我們有三個數組聲明。 聲明由兩部分組成。 數組的類型和名稱。 數組的類型具有確定數組中元素的類型(在我們的情況下為`int`,`String`,`float`)和一對方括號`[]`的數據類型。 方括號表示我們有一個數組。 集合具有相似的目的。 它們比數組更強大。 稍后將在單獨的章節中進行介紹。 ## C# 初始化數組 有幾種方法,如何在 C# 中初始化數組。 `Program.cs` ```cs using System; namespace InitArray { class Program { static void Main(string[] args) { int[] array = new int[5]; array[0] = 1; array[1] = 2; array[2] = 3; array[3] = 4; array[4] = 5; for (int i = 0; i < array.Length; i++) { Console.WriteLine(array[i]); } } } } ``` 我們聲明并初始化一個數值數組。 數組的內容將打印到控制臺。 ```cs int[] array = new int[5]; ``` 在這里,我們聲明一個包含五個元素的數組。 所有元素都是整數。 ```cs array[0] = 1; array[1] = 2; ... ``` 我們用一些數據初始化數組。 這是分配初始化。 索引在方括號中。 數字 1 將成為數組的第一個元素,數字 2 將成為第二個元素。 ```cs for (int i = 0; i < array.Length; i++) { Console.WriteLine(array[i]); } ``` 我們遍歷數組并打印其元素。 數組具有`Length`屬性,該屬性給出數組中元素的數量。 由于數組基于零,因此索引為`0..length-1`。 我們可以在一個語句中聲明并初始化一個數組。 `Program.cs` ```cs using System; namespace InitArray2 { class Program { static void Main(string[] args) { int[] array = new int[] { 2, 4, 5, 6, 7, 3, 2 }; foreach (int i in array) { Console.WriteLine(i); } } } } ``` 這是先前程序的修改版本。 ```cs int[] array = new int[] {2, 4, 5, 6, 7, 3, 2 }; ``` 一步就聲明并初始化一個數組。 元素在大括號中指定。 我們沒有指定數組的長度。 編譯器將為我們完成此任務。 ```cs foreach (int i in array) { Console.WriteLine(i); } ``` 我們使用`foreach`關鍵字遍歷數組并打印其內容。 ## C# 數組訪問元素 創建數組后,可以通過其索引訪問其元素。 索引是放在數組名稱后面方括號內的數字。 我們可以使用末尾`^`運算符的索引從數組末尾獲取元素。 `^0`等于`array.Length`,`^n`等于`array.Length-n`。 `Program.cs` ```cs using System; namespace AccessElements { class Program { static void Main(string[] args) { string[] names = { "Jane", "Thomas", "Lucy", "David" }; Console.WriteLine(names[0]); Console.WriteLine(names[1]); Console.WriteLine(names[2]); Console.WriteLine(names[3]); Console.WriteLine("*************************"); Console.WriteLine(names[^1]); Console.WriteLine(names[^2]); Console.WriteLine(names[^3]); Console.WriteLine(names[^4]); } } } ``` 在示例中,我們創建一個字符串名稱數組。 我們通過其索引訪問每個元素,并將它們打印到終端。 ```cs string[] names = { "Jane", "Thomas", "Lucy", "David" }; ``` 將創建一個字符串數組。 ```cs Console.WriteLine(names[0]); Console.WriteLine(names[1]); Console.WriteLine(names[2]); Console.WriteLine(names[3]); ``` 數組的每個元素都打印到控制臺。 在`names[0]`構造中,我們引用了名稱數組的第一個元素。 ```cs Console.WriteLine(names[^1]); Console.WriteLine(names[^2]); Console.WriteLine(names[^3]); Console.WriteLine(names[^4]); ``` 我們從頭開始訪問數組元素。 ```cs $ dotnet run Jane Thomas Lucy David ************************* David Lucy Thomas Jane ``` 運行示例,我們得到上面的輸出。 ## C# 數組修改元素 可以修改數組的元素-它們不是不可變的。 `Program.cs` ```cs using System; namespace ModifyElements { class Program { static void Main(string[] args) { int[] vals = { 1, 2, 3, 4 }; vals[0] *= 2; vals[1] *= 2; vals[2] *= 2; vals[3] *= 2; Console.WriteLine("[{0}]", string.Join(", ", vals)); } } } ``` 我們有一個由三個整數組成的數組。 每個值都將乘以 2。 ```cs int[] vals = { 1, 2, 3, 4 }; ``` 創建一個由三個整數組成的數組。 ```cs vals[0] *= 2; vals[1] *= 2; vals[2] *= 2; vals[3] *= 2; ``` 使用元素訪問,我們將數組中的每個值乘以 2。 ```cs Console.WriteLine("[{0}]", string.Join(", ", vals)); ``` 使用`Join()`方法,我們從數組的所有元素中創建一個字符串。 元素用逗號分隔。 ```cs $ dotnet run [2, 4, 6, 8] ``` 所有四個整數均已乘以數字 2。 ## C# 數組切片 我們可以使用`..`運算符來獲取數組切片。 范圍指定范圍的開始和結束。 范圍的開始是包含范圍的,但范圍的結束是包含范圍的。 這意味著起點包括在范圍內,而終點不包括在范圍內。 `Program.cs` ```cs using System; namespace ArrayRanges { class Program { static void Main(string[] args) { int[] vals = { 1, 2, 3, 4, 5, 6, 7 }; int[] vals2 = vals[1..5]; Console.WriteLine("[{0}]", string.Join(", ", vals2)); int[] vals3 = vals[..6]; Console.WriteLine("[{0}]", string.Join(", ", vals3)); int[] vals4 = vals[3..]; Console.WriteLine("[{0}]", string.Join(", ", vals4)); } } } ``` 該示例適用于數組范圍。 ```cs int[] vals2 = vals[1..5]; ``` 我們創建一個數組切片,其中包含從索引 1 到索引 4 的元素。 ```cs int[] vals3 = vals[..6]; ``` 如果省略起始索引,則切片將從索引 0 開始。 ```cs int[] vals4 = vals[3..]; ``` 如果省略了結束索引,則切片將一直持續到數組的末尾。 ```cs $ dotnet run [2, 3, 4, 5] [1, 2, 3, 4, 5, 6] [4, 5, 6, 7] ``` 這是輸出。 ## C# 遍歷數組 我們經常需要遍歷數組的所有元素。 我們展示了兩種遍歷數組的常用方法。 `Program.cs` ```cs using System; namespace Traversing { class Program { static void Main(string[] args) { string[] planets = { "Mercury", "Venus", "Mars", "Earth", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto" }; for (int i=0; i < planets.Length; i++) { Console.WriteLine(planets[i]); } foreach (string planet in planets) { Console.WriteLine(planet); } } } } ``` 將創建一個行星名稱數組。 我們使用`for`和`foreach`語句來打印所有值。 ```cs for (int i=0; i < planets.Length; i++) { Console.WriteLine(planets[i]); } ``` 在此循環中,我們利用了可以從數組對象中獲取元素數量的事實。 元素數存儲在`Length`屬性中。 ```cs foreach (string planet in planets) { Console.WriteLine(planet); } ``` 在遍歷數組或其他集合時,可以使用`foreach`語句使代碼更緊湊。 在每個循環中,將行星變量傳遞給行星數組中的下一個值。 ## C# 數組大小 到目前為止,我們已經處理了一維數組。 指定元素所需的索引數稱為數組的維或等級。 ### 二維數組 接下來,我們將處理二維數組。 `Program.cs` ```cs using System; namespace TwoDimensions { class Program { static void Main(string[] args) { int[,] twodim = new int[,] { {1, 2, 3}, {1, 2, 3} }; int d1 = twodim.GetLength(0); int d2 = twodim.GetLength(1); for (int i=0; i<d1; i++) { for (int j=0; j<d2; j++) { Console.WriteLine(twodim[i, j]); } } } } } ``` 如果我們需要兩個索引來訪問數組中的元素,則我們有一個二維數組。 ```cs int[,] twodim = new int[,] { {1, 2, 3}, {1, 2, 3} }; ``` 我們在一個語句中聲明并初始化一個二維數組。 請注意方括號內的逗號。 ```cs int d1 = twodim.GetLength(0); int d2 = twodim.GetLength(1); ``` 我們得到數組的大小。 `GetLength()`獲取數組指定維中的元素數。 ```cs for (int i=0; i<d1; i++) { for (int j=0; j<d2; j++) { Console.WriteLine(twodim[i, j]); } } ``` 我們使用兩個`for`循環遍歷二維數組的所有元素。 請注意,使用兩個索引(以逗號分隔)可獲得特定的數組元素。 ```cs $ dotnet run 1 2 3 1 2 3 ``` 這是代碼示例的輸出。 我們可以使用`foreach`循環遍歷二維數組。 `Program.cs` ```cs using System; namespace TraversingTwoDim { class Program { static void Main(string[] args) { int[,] vals = new int[4, 2] { { 9, 99 }, { 3, 33 }, { 4, 44 }, { 1, 11 } }; foreach (var val in vals) { Console.WriteLine(val); } } } } ``` 通過`foreach`循環,我們從頭到尾一個一個地獲取元素。 ```cs $ dotnet run 9 99 3 33 4 44 1 11 ``` This is the output. ### 三維數組 接下來,我們將處理三維數組。 `Program.cs` ```cs using System; namespace ThreeDimensions { class Program { static void Main(string[] args) { int[,,] n3 = { {{12, 2, 8}}, {{14, 5, 2}}, {{3, 26, 9}}, {{4, 11, 2}} }; int d1 = n3.GetLength(0); int d2 = n3.GetLength(1); int d3 = n3.GetLength(2); for (int i=0; i<d1; i++) { for (int j=0; j<d2; j++) { for (int k=0; k<d3; k++) { Console.Write(n3[i, j, k] + " "); } } } Console.Write('\n'); } } } ``` 我們有一個數字三維數組。 同樣,我們用數字初始化數組并將其打印到終端。 ```cs int[,,] n3 = { {{12, 2, 8}}, {{14, 5, 2}}, {{3, 26, 9}}, {{4, 11, 2}} }; ``` 左側的方括號和右側的其他花括號之間還有另一個逗號。 ```cs for (int k=0; k<d3; k++) { Console.Write(n3[i, j, k] + " "); } ``` 該循環經過三維。 我們使用三個索引從數組中檢索值。 ```cs $ dotnet run 12 2 8 14 5 2 3 26 9 4 11 2 ``` 我們將三維數組的內容打印到控制臺。 ### 維數 有`Rank`屬性,它提供數組的維數。 `Program.cs` ```cs using System; namespace Rank { class Program { static void Main(string[] args) { int[] a1 = { 1, 2 }; int[,] a2 = { { 1 }, { 2 } }; int[,,] a3 = { { { 1, 2 }, { 2, 1 } } }; Console.WriteLine(a1.Rank); Console.WriteLine(a2.Rank); Console.WriteLine(a3.Rank); } } } ``` 我們有三個數組。 我們使用`Rank`屬性獲取每個大小的數量。 ```cs Console.WriteLine(a1.Rank); ``` 在這里,我們獲得第一個數組的排名。 ```cs $ dotnet run 1 2 3 ``` 這是程序的輸出。 ## C# 鋸齒狀數組 具有相同大小元素的數組稱為矩形數組。 相反,具有不同大小元素的數組稱為鋸齒狀數組。 鋸齒狀數組的聲明和初始化方式不同。 `Program.cs` ```cs using System; namespace Jagged { class Program { static void Main(string[] args) { int[][] jagged = new int[][] { new int[] { 1, 2 }, new int[] { 1, 2, 3 }, new int[] { 1, 2, 3, 4 } }; foreach (int[] array in jagged) { foreach (int e in array) { Console.Write(e + " "); } } Console.Write('\n'); } } } ``` 這是一個鋸齒狀數組的示例。 ```cs int[][] jagged = new int[][] { new int[] { 1, 2 }, new int[] { 1, 2, 3 }, new int[] { 1, 2, 3, 4 } }; ``` 這是鋸齒狀數組的聲明和初始化。 請注意,這一次,我們使用兩對方括號。 我們有一個數組數組。 更具體地說,我們已經聲明了一個數組,其中包含三個`int`數據類型的數組。 每個數組具有不同數量的元素。 ```cs foreach (int[] array in jagged) { foreach (int e in array) { Console.Write(e + " "); } } ``` 我們使用兩個`foreach`循環遍歷鋸齒狀數組。 在第一個循環中,我們得到數組。 在第二個循環中,我們獲取獲得的數組的元素。 ## C# 數組方法 有多種使用數組的方法。 這些方法可用于檢索,修改,排序,復制,搜索數據。 我們使用的這些方法是`Array`類的靜態方法或數組對象的成員方法。 `Program.cs` ```cs using System; namespace Sorting { class Program { static void Main(string[] args) { string[] names = {"Jane", "Frank", "Alice", "Tom" }; Array.Sort(names); foreach(string el in names) { Console.Write(el + " "); } Console.Write('\n'); Array.Reverse(names); foreach(string el in names) { Console.Write(el + " "); } Console.Write('\n'); } } } ``` 在此示例中,我們對數據進行排序。 ```cs string[] names = {"Jane", "Frank", "Alice", "Tom" }; ``` 我們有一個字符串數組。 ```cs Array.Sort(names); ``` 靜態`Sort()`方法按字母順序對數據進行排序。 ```cs Array.Reverse(names); ``` `Reverse()`方法反轉整個一維數組中元素的順序。 ```cs $ dotnet run Alice Frank Jane Tom Tom Jane Frank Alice ``` 我們已經按升序和降序對名稱進行了排序。 以下示例使用`SeValue()`,`GetValue()`,`IndexOf()`,`Copy()`和`Clear()`方法。 `Program.cs` ```cs using System; namespace ArrayMethods { class Program { static void Main(string[] args) { string[] names = {"Jane", "Frank", "Alice", "Tom"}; string[] girls = new string[5]; names.SetValue("Beky", 1); names.SetValue("Erzebeth", 3); Console.WriteLine(names.GetValue(1)); Console.WriteLine(names.GetValue(3)); Console.WriteLine(Array.IndexOf(names, "Erzebeth")); Array.Copy(names, girls, names.Length); foreach(string girl in girls) { Console.Write(girl + " "); } Console.Write('\n'); Array.Clear(names, 0, 2); foreach(string name in names) { Console.Write(name + " "); } Console.Write('\n'); } } } ``` 本示例介紹了其他方法。 ```cs names.SetValue("Beky", 1); names.SetValue("Erzebeth", 3); ``` `SetValue()`為數組中的特定索引設置一個值。 ```cs Console.WriteLine(names.GetValue(1)); Console.WriteLine(names.GetValue(3)); ``` 我們使用`GetValue()`方法從數組中檢索值。 ```cs Console.WriteLine(Array.IndexOf(names, "Erzebeth")); ``` `IndexOf()`方法返回首次出現特定值的索引。 ```cs Array.Copy(names, girls, names.Length); ``` `Copy()`方法將值從源數組復制到目標數組。 第一個參數是源數組,第二個參數是目標數組。 第三個參數是長度; 它指定要復制的元素數。 ```cs Array.Clear(names, 0, 2); ``` `Clear()`方法從數組中刪除所有元素。 它包含三個參數:數組,起始索引和要從索引中清除的元素數。 ```cs $ dotnet run Beky Erzebeth 3 Jane Beky Alice Erzebeth Alice Erzebeth ``` This is the output. 在 C# 教程的這一部分中,我們使用了數組。 {% endraw %}
                  <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>

                              哎呀哎呀视频在线观看