「C Sharpの基礎 - ジャグ配列」の版間の差分
		
		
		
		
		
		ナビゲーションに移動
		検索に移動
		
				
		
 
 
		
	
| 細 (Wiki がページ「ジャグ配列(C Sharp)」を「C Sharpの基礎 - ジャグ配列」に、リダイレクトを残さずに移動しました) | 
2020年4月21日 (火) 22:46時点における版
ジャグ配列とは
ジャグ配列とは、配列の中の要素が配列となっている配列である。
そのため、配列の配列と呼ばれることがある。
ジャグ配列の要素(配列の中の配列)において、要素数が異なっていても問題ない。
配列の宣言と初期化
ジャグ配列の宣言と初期化は以下のように行うことができる。
ここでは、以下のような表をジャグ配列で扱うことを考える。
表1. ジャグ配列
| 名前 | 性別 | 特技 | |
|---|---|---|---|
| [0] | [1] | [2] | |
| [0] | 山田 | 男 | 剣道 | 
| [1] | 鈴木 | ||
| [2] | 佐藤 | 女 | |
| [3] | 田中 | 男 | 野球 | 
表2. 対応するインデックス
| [0] | [1] | [2] | |
|---|---|---|---|
| [0] | [0][0] | [0][1] | [0][2] | 
| [1] | [1][0] | ||
| [2] | [2][0] | [2][1] | |
| [3] | [3][0] | [3][1] | [3][2] | 
 // ジャグ配列の宣言と初期化
 string[][] strJagStudent = {
                             new string[]{ “山田”, “男”, “剣道” },
                             new string[]{ “鈴木” },
                             new string[]{ “佐藤”, “女” },
                             new string[]{ “田中”, “男”, “野球” }
                            };
 
 // 以下の記述でも可能
 string[][] strJagStudent = {
                             new string[3]{ “山田”, “男”, “剣道” },
                             new string[1]{ “鈴木” },
                             new string[2]{ “佐藤”, “女” },
                             new string[3]{ “田中”, “男”, “野球” }
                            };
 
 // 以下の記述でも可能
 string[][] strJagStudent = new string[5][];
 strJagStudent[0] = new string[] { “山田”, “男”, “剣道” };
 strJagStudent[1] = new string[] { “鈴木” };
 strJagStudent[2] = new string[] { “佐藤”, “女” };
 strJagStudent[3] = new string[] { “田中”, “男”, “野球” };
 
 // 以下の記述でも可能
 string[][] strJagStudent = new string[5][];
 strJagStudent[0] = new string[3];
 strJagStudent[0][0] = “山田”;
 strJagStudent[0][1] = “男”;
 strJagStudent[0][2] = “剣道”;
 strJagStudent[1] = new string[1];
 strJagStudent[1][0] = “鈴木”;
 strJagStudent[2] = new string[2];
 strJagStudent[2][0] = “佐藤”;
 strJagStudent[2][1] = “女”;
 strJagStudent[3] = new string[3];
 strJagStudent[3][0] = “田中”;
 strJagStudent[3][1] = “男”;
 strJagStudent[3][2] = “野球”;
ジャグ配列の要素を出力
配列の中の配列の要素を出力したい場合は、以下のように、ループ文が2つ必要になる。
strJagStudent.Lengthはジャグ配列の長さ(要素数)を意味する。ここでは、値は5になる。
strJagStudent[i].Lengthは配列の中の配列の長さ(要素数)を意味する。ここでは、配列の長さに応じて、値は1~3をとる。
 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 
 namespace Jagged
 {
    class Program
    {
       static void Main(string[] args)
       {
          string[][] strJagStudent = {
                                      new string[3]{ “山田”, “男”, “剣道” },
                                      new string[1]{ “鈴木” },
                                      new string[2]{ “佐藤”, “女” },
                                      new string[3]{ “田中”, “男”, “野球” }
                                     };
 
          for (int i = 0; i < strJagStudent.Length; i++)
          {
             for (int j = 0; j < strJagStudent[i].Length; j++)
             {
                Console.Write(strJagStudent[i][j] + ":");
             }
             Console.WriteLine();
             Console.WriteLine("------------");
          }
       }
    }
 }
2次元以上の配列を要素にとるジャグ配列
ジャグ配列は2次元の配列を要素として扱うことができる。
例えば、以下のようなジャグ配列を宣言することができる。
 int[][,] jagged_array = new int[3][,]
                        {
                         new int[,]{ {2,5}, {77,30}, {22,55} },
                         new int[,]{ {8,10} },
                         new int[,]{ {32,53}, {7,3} }
                        };