端くれプログラマの備忘録 C# [C#] 多次元配列を確保する

[C#] 多次元配列を確保する

多次元配列の確保する方法。

配列の確保

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

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

配列の初期化

// 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 }
    }
};

多次元配列 (C# プログラミング ガイド)
http://msdn.microsoft.com/ja-jp/library/2yd9wwz4.aspx