首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> C# >> 關於C#

C# 語法練習 (7):陣列完整指南

     編輯:關於C#

C# 陣列基礎與進階用法

陣列是 C# 中最基礎的資料結構之一,用於儲存相同型別的多個元素。本文將透過豐富的程式碼範例,詳細說明一維陣列、多維陣列、交錯陣列的宣告、初始化與操作技巧。

一、一維陣列的宣告與初始化

C# 提供多種建立陣列的方式,開發者可根據情境選擇最適合的寫法。

1. 字串陣列

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();
    }
}

2. 整數陣列(簡潔寫法)

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();
    }
}

3. 指定長度並初始化

初始化時維度可以省略;若不省略,長度必須與元素個數一致。

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();
    }
}

二、分步驟宣告與指派

1. 先宣告長度,稍後指派值

using System;

class MyClass
{
    static void Main()
    {
        int[] arr = new int[3]; // 預設值為 0

        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();
    }
}

2. 先宣告變數,稍後以 new 關鍵字建立

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();
    }
}

3. 重新指派陣列(改變長度)

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();
    }
}

四、多維陣列

1. 二維陣列初始化

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();
    }
}

2. 二維陣列逐一指派

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();
    }
}

3. 三維陣列範例

using System;

class MyClass
{
    static void Main()
    {
        int[,,] arr = new int[2, 3, 4];

        for (int x = 0; x < 2; x++)
            for (int y = 0; y < 3; y++)
                for (int z = 0; z < 4; z++)
                    arr[x, y, z] = Convert.ToInt32(Convert.ToString(x + 1) + Convert.ToString(y + 1) + Convert.ToString(z + 1));

        foreach (int i in arr) Console.WriteLine(i);

        Console.ReadKey();
    }
}

五、交錯陣列

交錯陣列是「陣列的陣列」,每一維的長度可以不同。

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();
    }
}

總結

  • 一維陣列支援多種初始化語法,靈活度高。
  • 多維陣列使用逗號分隔維度(如 int[,]),記憶體連續配置。
  • 交錯陣列(int[][])適合不規則資料結構,每一維可獨立配置長度。
  • 善用 foreach 迴圈可簡化陣列遍歷程式碼。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved