aspose file tools*
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 Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "multi-dimensional array question" Watch "multi-dimensional array question" New topic
Author

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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: multi-dimensional array question