Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

multi-dimensional array question

 
Joe McGuire
Ranch Hand
Posts: 293
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 358
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Array constants can only be used in initializers.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic