This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes multi-dimensional array question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "multi-dimensional array question" Watch "multi-dimensional array question" New topic

multi-dimensional array question

Joe McGuire
Ranch Hand

Joined: Mar 19, 2001
Posts: 293
I have an interesting question that arose while trying to help someone in another forum.
The following code:
int[][] a = { { 1 , 2 } , { 1 , 2 } } ;
compiles, of course, without problem.
However, if we split the declaration and the initialization, it fails:
int[][] a ;
a = { { 1 , 2 } , { 1 , 2 } } ;
In single-dimensional arrays this is no problem. Why doesn't it work with multi-dimensional arrays?
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

If you try to do this:
it gives an 'illegal start of expression' for the opening brace.

I think this syntax can only be used during declarative initialization. Otherwise, you must explicity initialize each array element (and this goes for both 1 and n-dimensional arrays.)
rani bedi
Ranch Hand

Joined: Feb 06, 2001
Posts: 358
Array constants can only be used in initializers.

Cheers,<br />Rani<br />SCJP, SCWCD, SCBCD
I agree. Here's the link:
subject: multi-dimensional array question
It's not a secret anymore!