2
2

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 5 years have passed since last update.

配列初期化時のエラー?

Posted at

配列初期化時のエラー?

今回の現象:String配列を一回宣言してから初期化しようとしたらエラー

String[] strList={"1","2","3","4"};

これができるならこれは...

String[] strList;
strList={"1","2","3","4"};

こんな感じで初期化できると思いきや...
Array constants can only be used in initializers

じゃあこれは...

String[] strList;
strList=new String[4];
	strList[0] = "1";
	strList[1] = "2";
	strList[2] = "3";
	strList[3] = "4";

成功!!

でも長ったらしいので不満...

ということで調べると
http://d.hatena.ne.jp/Kappuccino/20080721/1216623546

こういう書き方あるのか...

String[] strList;
strList=new String[]{"1","2","3","4"};

なるほど!

String[] strList={"1","2","3","4"};

これは内部的になにか処理が違うのか

java始めたばかりだから勉強だなー

2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?