C# 文法練習(7): 數組

來源:互聯網
上載者:User
字串數組:
using System;class MyClass{    static void Main()    {        string[] arr = new string[3] { "aa", "bb", "cc" };        foreach (string s in arr) Console.WriteLine(s); // aa/bb/cc        Console.ReadKey();    }}

整數數組:

using System;class MyClass{    static void Main()    {        int[] arr = { 11, 22, 33 };        foreach (int i in arr) Console.WriteLine(i); // 11/22/33        Console.ReadKey();    }}

初始化時維數可以省略; 若不省略, 得一致:

using System;class MyClass{    static void Main()    {        int[] arr = new int[3] { 11, 22, 33 };        foreach (int i in arr) Console.WriteLine(i); // 11/22/33        Console.ReadKey();    }}

聲明同時指定維數, 但暫不賦值:

using System;class MyClass{    static void Main()    {        int[] arr = new int[3];        foreach (int i in arr) Console.WriteLine(i); // 0/0/0        arr[0] = 11;        arr[1] = 22;        arr[2] = 33;        foreach (int i in arr) Console.WriteLine(i); // 11/22/33        Console.ReadKey();    }}

先聲明, 賦值時再確定維數:

using System;class MyClass{    static void Main()    {        int[] arr;        arr = new int[] { 11, 22, 33 };        foreach (int i in arr) Console.WriteLine(i); // 11/22/33        Console.ReadKey();    }}

可改變聲明時的維數:

using System;class MyClass{    static void Main()    {        int[] arr = new int[3];        arr = new int[4] { 11, 22, 33, 44 };        foreach (int i in arr) Console.WriteLine(i); // 11/22/33/44        Console.ReadKey();    }}

如果用變數做數組維數, 一定要是 const:

using System;class MyClass{    static void Main()    {        const int size = 3;        int[] arr = new int[size] { 11, 22, 33};        foreach (int i in arr) Console.WriteLine(i); // 11/22/33        Console.ReadKey();    }}

二維數組初始化:

using System;class MyClass{    static void Main()    {        int[,] arr = { {11,12,13,14}, {21,22,23,24}, {31,32,33,34} };        foreach (int i in arr) Console.WriteLine(i);        Console.ReadKey();    }}

二維數組賦值:

using System;class MyClass{    static void Main()    {        int[,] arr = new int[3, 4];        arr[0,0] = 11;        arr[0,1] = 12;        arr[0,2] = 13;        arr[0,3] = 14;        arr[1,0] = 21;        arr[1,1] = 22;        arr[1,2] = 23;        arr[1,3] = 24;        arr[2,0] = 31;        arr[2,1] = 32;        arr[2,2] = 33;        arr[2,3] = 34;        foreach (int i in arr) Console.WriteLine(i);        Console.ReadKey();    }}

多維陣列:

using System;class MyClass{    static void Main()    {        int[,,] arr = new int[2, 3, 4];        for (int x = 0; x 

數組中的數組:

using System;class MyClass{    static void Main()    {        int[][] arr = new int[3][];        arr[0] = new int[2] { 11, 12 };        arr[1] = new int[3] { 21, 22, 23 };        arr[2] = new int[4] { 31, 32, 33, 34 };        foreach (int[] ns in arr) foreach (int n in ns)                Console.WriteLine(n);        Console.ReadKey();    }}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.