不規則陣列 (C# 程式設計手冊)Jagged Arrays (C# Programming Guide)

不規則陣列是一種陣列,其元素也是陣列。A jagged array is an array whose elements are arrays. 不規則陣列的項目可以有不同的維度和大小。The elements of a jagged array can be of different dimensions and sizes. 不規則陣列有時稱為「陣列的陣列」。A jagged array is sometimes called an "array of arrays." 下列範例示範如何宣告、初始化和存取不規則陣列。The following examples show how to declare, initialize, and access jagged arrays.

下列是具有三個項目的一維陣列宣告,且每個都是整數的一維陣列:The following is a declaration of a single-dimensional array that has three elements, each of which is a single-dimensional array of integers:

int[][] jaggedArray = new int[3][];

必須先初始化 jaggedArray 的項目,才能予以使用。Before you can use jaggedArray, its elements must be initialized. 您可以初始化項目,如下所示:You can initialize the elements like this:

jaggedArray[0] = new int[5];
jaggedArray[1] = new int[4];
jaggedArray[2] = new int[2];

每個項目都是整數的一維陣列。Each of the elements is a single-dimensional array of integers. 第一個項目是 5 個整數的陣列、第二個是 4 個整數的陣列,而第三個是 2 個整數的陣列。The first element is an array of 5 integers, the second is an array of 4 integers, and the third is an array of 2 integers.

也可以使用初始設定式將值填入陣列元素,在此情況下,您不需要陣列大小。It is also possible to use initializers to fill the array elements with values, in which case you do not need the array size. 例如:For example:

jaggedArray[0] = new int[] { 1, 3, 5, 7, 9 };
jaggedArray[1] = new int[] { 0, 2, 4, 6 };
jaggedArray[2] = new int[] { 11, 22 };

您也可以在宣告時初始化陣列,如下所示:You can also initialize the array upon declaration like this:

int[][] jaggedArray2 = new int[][]
{
    new int[] { 1, 3, 5, 7, 9 },
    new int[] { 0, 2, 4, 6 },
    new int[] { 11, 22 }
};

您可以使用下列簡短格式。You can use the following shorthand form. 請注意,您不能從項目初始化省略 new 運算子,因為沒有項目的預設初始化:Notice that you cannot omit the new operator from the elements initialization because there is no default initialization for the elements:

int[][] jaggedArray3 =
{
    new int[] { 1, 3, 5, 7, 9 },
    new int[] { 0, 2, 4, 6 },
    new int[] { 11, 22 }
};

不規則陣列為陣列的陣列,因此其元素為參考類型,且會初始化為 nullA jagged array is an array of arrays, and therefore its elements are reference types and are initialized to null.

您可以存取個別陣列元素,例如下列範例:You can access individual array elements like these examples:

// Assign 77 to the second element ([1]) of the first array ([0]):
jaggedArray3[0][1] = 77;

// Assign 88 to the second element ([1]) of the third array ([2]):
jaggedArray3[2][1] = 88;

可以混合使用不規則陣列與多維陣列。It is possible to mix jagged and multidimensional arrays. 以下是一維不規則陣列的宣告和初始化,而此陣列包含三個不同大小的二維陣列元素。The following is a declaration and initialization of a single-dimensional jagged array that contains three two-dimensional array elements of different sizes. 如需二維陣列的詳細資訊,請參閱多維陣列For more information about two-dimensional arrays, see Multidimensional Arrays.

int[][,] jaggedArray4 = new int[3][,]
{
    new int[,] { {1,3}, {5,7} },
    new int[,] { {0,2}, {4,6}, {8,10} },
    new int[,] { {11,22}, {99,88}, {0,9} }
};

如此範例所示,您可以存取個別項目,其中會顯示第一個陣列的項目 [1,0] 值 (值 5):You can access individual elements as shown in this example, which displays the value of the element [1,0] of the first array (value 5):

System.Console.Write("{0}", jaggedArray4[0][1, 0]);

Length 方法會傳回不規則陣列中所含的陣列數目。The method Length returns the number of arrays contained in the jagged array. 例如,假設您已經宣告先前的陣列,如下行所示:For example, assuming you have declared the previous array, this line:

System.Console.WriteLine(jaggedArray4.Length);

會傳回值 3。returns a value of 3.

範例Example

此範例會建置其項目本身為陣列的陣列。This example builds an array whose elements are themselves arrays. 每個陣列元素都會有不同的大小。Each one of the array elements has a different size.

class ArrayTest
{
    static void Main()
    {
        // Declare the array of two elements.
        int[][] arr = new int[2][];

        // Initialize the elements.
        arr[0] = new int[5] { 1, 3, 5, 7, 9 };
        arr[1] = new int[4] { 2, 4, 6, 8 };

        // Display the array elements.
        for (int i = 0; i < arr.Length; i++)
        {
            System.Console.Write("Element({0}): ", i);

            for (int j = 0; j < arr[i].Length; j++)
            {
                System.Console.Write("{0}{1}", arr[i][j], j == (arr[i].Length - 1) ? "" : " ");
            }
            System.Console.WriteLine();
        }
        // Keep the console window open in debug mode.
        System.Console.WriteLine("Press any key to exit.");
        System.Console.ReadKey();
    }
}
/* Output:
    Element(0): 1 3 5 7 9
    Element(1): 2 4 6 8
*/

另請參閱See also