<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>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                > c#中的數組都是由systerm.array類派生而來的引用對象。 ![](https://ws4.sinaimg.cn/large/006tKfTcgy1frahs2ftioj30lf0byjts.jpg) # 一維數組表達 用new給數組初始化。 ``` int[] arr = new int[3]; // 只定義數組的長度,不定義具體內容,是可行的 ``` # 常見錯誤 1. `int arr[] = new int[3];` 錯在[]位置寫錯,應該是int[] arr 2. `int[] arr = new int[3] {1,2 }` 錯在如果定義了數組長度是3,但初始化的時候只有2個數,那么會提示錯誤。 3. `int[] arr = new string[3]` 錯在左邊定義了int類型,右邊又定義了string類型,相沖突。 ## 例如:輸入每個月天數 并輸出 ``` int[] arr = new int[12] {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 30} int i; // 用來指代數組第n個數 for(i=0, i<12; i++) { console.writeline( (i+1) + "月份為" + arr[i]); //數組編號從0開始,故i編號為0-11. console.write 的表達 } console.readline(); ``` ![](https://box.kancloud.cn/6afc05640ff8dec596c337ed22a77478_673x331.png) ![](https://box.kancloud.cn/3153e835bd3cce06863c6b1f39c1212a_677x442.png) # 二維數組表達 `int [ , ] ` 或` string [] []` 一般用第一種方法 `int[,] arr = new int [2, ]` 因為第二種方法也叫不規則數組,沒法指定列數。否則代碼錯。 CS0178 C# Invalid rank specifier: expected ',' or ']' 無效的秩說明符:應為“,”或“]” ``` int[, ] arr = new int[2, ]; //不指定列數 a[0] = new int[2]; //第0行有2列 a[1] = new int[4]; //第1行有4列 ``` ![](https://box.kancloud.cn/2515c5b05b0b922e5e5753967f21ddc2_725x510.png) 數組可以有多維。 ## 初始化二維數組 ``` int[ , ] arr = {{12,12}, {13,13}, {14,14}}; //這里右邊不能再寫new,否則錯誤 int[ , ] arr = new int[] {{12,12}, {13,13}, {14,14}}; //這樣才對 ``` ![](https://box.kancloud.cn/bb384108c5da50edcef5894663fc5ee4_1059x607.png) ## 數組轉置 比如 ![](https://box.kancloud.cn/f360799d627f0e3053131abd3c41baba_131x61.png) ![](https://box.kancloud.cn/7f7c20828c428ea6b34c42c43afae3b4_885x747.png) ``` int[,] arr = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; int i, j; Console.WriteLine("原始數組"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { Console.Write(arr[i, j] + " "); } Console.WriteLine(); } int temp; for(i=0;i<3;i++) { for(j=0;j<i;j++) { temp = arr[i, j]; arr[i, j] = arr[j, i]; arr[j, i] = temp; } } Console.WriteLine("修改后的數組"); for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { Console.Write(arr[i, j] + " "); } Console.WriteLine(); } Console.ReadLine(); ``` # 直接調用systerm.array中的方法 ``` int[,] arr = new int[,]{}; console.writeline(arr.length); ``` ![](https://ws3.sinaimg.cn/large/006tKfTcly1frai0w057zj30bs01b0sq.jpg) 顯示長度為9 ![](https://ws2.sinaimg.cn/large/006tKfTcly1frai4o9c5oj304103a0sp.jpg) ![](https://box.kancloud.cn/d9d5a7119ccaa93cf2aa59ece634c02a_836x398.png) copy copy to exists getlength getvalue reverse setvalue sort ## 正序排序 Array.Sort(arr); ## 反轉排序 Array.Reverse; ## 倒序排序 Array.Sort(arr); Array.Reverse; # foreach 遍歷數組來輸出 ``` static void Main(string[] args) { int[,] arr = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; int i, j; Console.WriteLine("原始數組"); foreach (int a in arr) // 這里的a只是我們隨意定義的一個代詞用來表示arr中的元素,也可以是其他名字 { Console.Write(a + " "); } Console.WriteLine("arr's length: "+arr.Length); Console.ReadLine(); } ``` 但這么執行以后,顯示的就不是二維數組而是一維數組。 ![](https://ws3.sinaimg.cn/large/006tKfTcly1fraikp1v7lj308b01vq2z.jpg)
                  <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>

                              哎呀哎呀视频在线观看