多維陣列 (C# 程式設計手冊)Multidimensional Arrays (C# Programming Guide)

陣列可以有多個維度。Arrays can have more than one dimension. 例如,下列宣告會建立具有四個資料列和兩個資料行的二維陣列。For example, the following declaration creates a two-dimensional array of four rows and two columns.

int[,] array = new int[4, 2];

下列宣告會建立三維 (4、2 和 3) 陣列。The following declaration creates an array of three dimensions, 4, 2, and 3.

int[, ,] array1 = new int[4, 2, 3];

陣列初始化Array Initialization

您可以在宣告後初始化陣列,如下列範例所示。You can initialize the array upon declaration, as is shown in the following example.

// Two-dimensional array.
int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
// The same array with dimensions specified.
int[,] array2Da = new int[4, 2] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
// A similar array with string elements.
string[,] array2Db = new string[3, 2] { { "one", "two" }, { "three", "four" },
                                        { "five", "six" } };

// Three-dimensional array.
int[, ,] array3D = new int[,,] { { { 1, 2, 3 }, { 4, 5, 6 } },
                                 { { 7, 8, 9 }, { 10, 11, 12 } } };
// The same array with dimensions specified.
int[, ,] array3Da = new int[2, 2, 3] { { { 1, 2, 3 }, { 4, 5, 6 } },
                                       { { 7, 8, 9 }, { 10, 11, 12 } } };

// Accessing array elements.
System.Console.WriteLine(array2D[0, 0]);
System.Console.WriteLine(array2D[0, 1]);
System.Console.WriteLine(array2D[1, 0]);
System.Console.WriteLine(array2D[1, 1]);
System.Console.WriteLine(array2D[3, 0]);
System.Console.WriteLine(array2Db[1, 0]);
System.Console.WriteLine(array3Da[1, 0, 1]);
System.Console.WriteLine(array3D[1, 1, 2]);

// Getting the total count of elements or the length of a given dimension.
var allLength = array3D.Length;
var total = 1;
for (int i = 0; i < array3D.Rank; i++) {
    total *= array3D.GetLength(i);
}
System.Console.WriteLine("{0} equals {1}", allLength, total);

// Output:
// 1
// 2
// 3
// 4
// 7
// three
// 8
// 12
// 12 equals 12

您也可以在不指定順位的情況下初始化陣列。You can also initialize the array without specifying the rank.

int[,] array4 = { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };

如果選擇在不進行初始化的情況下宣告變數,則必須使用 new 運算子將陣列指派給變數。If you choose to declare an array variable without initialization, you must use the new operator to assign an array to the variable. 下列範例會顯示 new 的用法。The use of new is shown in the following example.

int[,] array5;
array5 = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };   // OK
//array5 = {{1,2}, {3,4}, {5,6}, {7,8}};   // Error

下列範例會將值指派給特定的陣列元素。The following example assigns a value to a particular array element.

array5[2, 1] = 25;

同樣地,下列範例會取得特定陣列元素的值,並將它指派給 elementValue 變數。Similarly, the following example gets the value of a particular array element and assigns it to variable elementValue.

int elementValue = array5[2, 1];

下列程式碼範例會將陣列元素初始化為預設值 (除了不規則陣列外)。The following code example initializes the array elements to default values (except for jagged arrays).

int[,] array6 = new int[10, 10];

另請參閱See also