LoginSignup
10
9

More than 5 years have passed since last update.

javaで動的な二次元配列の作り方

Last updated at Posted at 2014-06-27

二次元配列[i][j]において、iは固定長でjが可変長な二次元配列を作る方法。
簡単に表すとこんな感じ

1 2 3 4 5 6 7 8 9
1 - - - - - - -
2 - - -
3 - - - - - - - - -
4 - -
5 - - - - - -

縦がi,横がj,-が配列の各要素

流れ的には
①二次元配列[5][]を作る
②ArrayListを使って、i行目の可変長部分を取得
③ArrayListから配列に戻す

実際にはこんな感じ
①String[][] s = new String[5][]

②ArrayList list = new ArrayList();
 でリストを定義
list.add("")をi行目のj分だけfor文で回す

③String[i] s = (String[])list.toArray(new String[0]);
 をiの数だけfor文でまわす

10
9
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
10
9