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の配列

Last updated at Posted at 2020-07-01

##はじめに
順次進行、繰り返し、条件分岐でだいたい作れるって言われても全然作れんわッッ!!(本投稿に関係はない)

##配列
変数は1つの値しか入れられないが、配列は複数の値をまとめて入れれる。
1列のものを一次元配列、2列以上は多次元配列という。
ひとつひとつを要素といい、その数を要素数という

kane.java
データ型[] 配列変数名;
配列変数名 = new データ型[要素数];

String[] banana;
banana = new string[3];

banana[0] = "Cavendish"
banana[1] = "Lakatan"
banana[2] = "GrosMichel"

上記は宣言と代入を別に記述
下記は宣言と代入を同時に

kane.java
データ型[] 配列変数名 = {要素1,要素2,...};

String[] banana = {"Cavendish", "Lakatan", "GrosMichel"}

[]はデータ型の後くっつけて書く(**int[],String[]**のように)

要素の上書きは、配列変数名[インデックス番号] = "新しい要素"

各要素は、配列名[インデックス番号]で取得できる。

kane.java
String[] banana = {"Cavendish", "Lakatan", "GrosMichel"};

banana[1] = "LadyFinger" //要素の上書き

System.out.println(banana[1]);

//実行結果 LadyFinger

##多次元配列
エクセルの表計算ソフト的な

|banana|0|1|
|:--|:--|:--|:--|
|0|Cavendish|Lakatan|
|1|GrosMichel|LadyFinger|

kane.java
データ型[][] 配列変数名;
配列変数名 = new データ型[要素数][要素数];

String[][] banana;
banana = new string[2][2];

banana[0][0] = "Cavendish"
banana[0][1] = "Lakatan"
banana[1][0] = "GrosMichel"
banana[1][1] = "LadyFinger"

System.out.println(banana[1][0]);

//実行結果 GrosMichel

これも省略できる

kane.java
String[][] banana = {{"Cavendish", "Lakatan"},{"GrosMichel","LadyFinger"}};

System.out.println(banana[0][0]);

//実行結果 Cavendish

##終わりに
多次元配列って便利なのはわかるが自分の頭が一次元だからパンクする

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?