VOOZH about

URL: https://qiita.com/shyu/items/29e8d4c2b1ae9ce29dce

⇱ javaで動的な二次元配列の作り方 #Java - Qiita


👁 Image
9

Go to list of users who liked

8

Share on X(Twitter)

Share on Facebook

Add to Hatena Bookmark

More than 5 years have passed since last update.

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

9
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文でまわす

9

Go to list of users who liked

8
0

Go to list of comments

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
9

Go to list of users who liked

8