0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[Java基礎⑧] 二次元配列の基本・for文/拡張for文で出力

Posted at

初めに

今回、二次元配列を学習し、その中身をfor文で取り出すということを 行いました。

二次元配列とは

2つのインデックスで要素を指定する配列のこと
//ひとまず3つの配列を用意します。
String[] teamA = {"りんご","なし","洋梨"};
String[] teamB = {"青森","岩手","宮城"};
String[] teamC = {"お皿","コップ","茶碗"};

//配列に配列を格納します。
String[][] teams = {teamA,teamB,teamC}

//以下では、「りんご」が出力されます。
System.out.print(teams[0][0])
//以下では、「青森」が出力されます。
System.out.print(teams[1][0])
//以下では、「コップ」が出力されます。
System.out.print(teams[2][1])

//インデックスの更新  「りんご」→「みかん」へ
team[0][0] = "みかん";
//「みかん」が出力されます。
System.out.print(teams[0][0]);

//配列の長さを調べる →「3」と出力されます。
System.out.println(teams.length);

//配列の中身の配列の長さをインデックスを使い求める →「3」と出力されます。
System.out.println(teams[0].length);

二次元配列のループ処理

for文で中身を全部出力する

String[][]teams ={{"りんご","なし"},{"青森","岩手"},{"お皿,"コップ"}};

for(int i = 0; i<teams.length; i++){
 for(int j = 0; j<teams[i].length; j++){
     System.out.println(teams[i][j]);
}
}

拡張for文

for(String[]team : teams){
 for(String item : team){
 System.out.println(item);

終わり

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?