MochiuWiki : SUSE, EC, PCB
案内
メインページ
最近の更新
おまかせ表示
MediaWiki についてのヘルプ
ツール
リンク元
関連ページの更新状況
特別ページ
ページ情報
We ask for
Donations
検索
個人用ツール
ログイン
Toggle dark mode
名前空間
ページ
議論
表示
閲覧
ソースを閲覧
履歴を表示
C Sharpの基礎 - ジャグ配列のソースを表示
提供: MochiuWiki : SUSE, EC, PCB
←
C Sharpの基礎 - ジャグ配列
あなたには「このページの編集」を行う権限がありません。理由は以下の通りです:
この操作は、次のグループのいずれかに属する利用者のみが実行できます:
管理者
、new-group。
このページのソースの閲覧やコピーができます。
== ジャグ配列とは == ジャグ配列とは、配列の中の要素が配列となっている配列である。<br> そのため、配列の配列と呼ばれることがある。<br> ジャグ配列の要素(配列の中の配列)において、要素数が異なっていても問題ない。<br><br> == 配列の宣言と初期化 == ジャグ配列の宣言と初期化は以下のように行うことができる。<br> ここでは、以下のような表をジャグ配列で扱うことを考える。<br> <br> <center> '''表1. ジャグ配列''' {| class="wikitable" |- ! !! 名前 !! 性別 !! 特技 |- | || [0] || [1] || [2] |- | [0] || 山田 || 男 || 剣道 |- | [1] || 鈴木 || || |- | [2] || 佐藤 || 女 || |- | [3] || 田中 || 男 || 野球 |} </center> <br> <center> '''表2. 対応するインデックス''' {| class="wikitable" |- ! !! [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] |} </center> <br> <syntaxhighlight lang="c#"> // ジャグ配列の宣言と初期化 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] = “野球”; </syntaxhighlight> <br><br> == ジャグ配列の要素を出力 == 配列の中の配列の要素を出力したい場合は、以下のように、ループ文が2つ必要になる。<br> strJagStudent.Lengthはジャグ配列の長さ(要素数)を意味する。ここでは、値は5になる。<br> strJagStudent[i].Lengthは配列の中の配列の長さ(要素数)を意味する。ここでは、配列の長さに応じて、値は1~3をとる。<br> <br> <syntaxhighlight lang="c++"> 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("------------"); } } } } </syntaxhighlight> <br><br> == 2次元以上の配列を要素にとるジャグ配列 == ジャグ配列は2次元の配列を要素として扱うことができる。<br> 例えば、以下のようなジャグ配列を宣言することができる。<br> <br> <syntaxhighlight lang="c#"> int[][,] jagged_array = new int[3][,] { new int[,]{ {2,5}, {77,30}, {22,55} }, new int[,]{ {8,10} }, new int[,]{ {32,53}, {7,3} } }; </syntaxhighlight> <br><br> __FORCETOC__ [[カテゴリ:C_Sharp]]
C Sharpの基礎 - ジャグ配列
に戻る。
案内
メインページ
最近の更新
おまかせ表示
MediaWiki についてのヘルプ
ツール
リンク元
関連ページの更新状況
特別ページ
ページ情報
We ask for
Donations
Collapse