2009年12月6日日曜日

2次元、3次元配列(以下の場合はそれぞれ3x2,2x2x2)を定義と同時に初期化する。

using System;

class MainClass
{
    static void Main()
    {
        // 2次元配列(3x2)の定義、初期化
        int[,] a = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 } };
        // 3次元配列(2x2x2)の定義、初期化
        int[, ,] b = new int[,,]{{{1,2},{3,4}},
            {{5,6},{7,8}}};
        // 2次元配列の参照
        // 出力値:1
        Console.WriteLine(a[0, 0]);
        // 出力値:2
        Console.WriteLine(a[0, 1]);
        // 出力値:3
        Console.WriteLine(a[1, 0]);
        // 出力値:4
        Console.WriteLine(a[1, 1]);
        // 出力値:5
        Console.WriteLine(a[2, 0]);
        // 出力値:6
        Console.WriteLine(a[2, 1]);
        // 3次元配列の参照
        // 出力値:1
        Console.WriteLine(b[0, 0, 0]);
        // 出力値:2
        Console.WriteLine(b[0, 0, 1]);
        // 出力値:3
        Console.WriteLine(b[0, 1, 0]);
        // 出力値:4
        Console.WriteLine(b[0, 1, 1]);
        // 出力値:5
        Console.WriteLine(b[1, 0, 0]);
        // 出力値:6
        Console.WriteLine(b[1, 0, 1]);
        // 出力値:7
        Console.WriteLine(b[1, 1, 0]);
        // 出力値:8
        Console.WriteLine(b[1, 1, 1]);
    }
}

0 コメント:

コメントを投稿