C#構文練習(7):配列

3960 ワード

文字列配列:


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

    }

}


 
   

整数配列:


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

    }

}


 
   

初期化時の次元数は省略できます.省略しない場合は、一致します.


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

    }

}


 
   

宣言により、次元数も指定されますが、値は指定されません.


using System;



class MyClass

{

    static void Main()

    {

        int[] arr = new int[3];



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

    }

}


 
   

先に宣言し、値を割り当てたときに次元数を決定します.


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

    }

}


 
   

変更可能宣言時の次元数:


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

    }

}


 
   

2 D配列の初期化:


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 D配列の割り当て:


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

    }

}


 
   

多次元配列:


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

    }

}