• Post Reply Bookmark Topic Watch Topic
  • New Topic

2D array multiple ways of declaration  RSS feed

 
Mike Tango
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Why there are different ways to declare 2D array ?

What's the real reason behind it ?

int[][] squares = new int[3][2];

int[][] multipliers = new int[10][]; ==> OK
int[][] multipliers = new int[][]; ==> Compile Error!

int[] quotes[] = new int[2][2];



Regards,
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are no ways to declare a 2D array because Java® doesn't support 2D arrays. What you are showing is arrays of arrays, which are better than 2D arrays.

Those declarations and initialisations are all different; when you use new Foo[xxx], you must supply a size for the array. You are only creating the outer array like that; its elements are all null. Whenever you create an array with the new operator, you must supply its size. So new int[][] doesn't supply a size and the object cannot be created. If you miss out the sizes for the inner arrays, you are leaving them blank (=null), or you can create all the inner arrays the same size as int[] quotes[] = new int[2][2]; That creates two arrays each of two elements, but the different placement of the [] is really bad style.

I prefer to use array initialisers if I can:-
int[][] matrix = {{1, 2}. {3, 4}, {5, 6}};
 
Mike Tango
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the feedback;

sorry I didn't get it ?

int[][] multipliers = new int[10][] works
int[][] multipliers = new int[][] doesn't work

And how to read the nextline:
int[] quotes[] = ....
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember that Java supports arrays of arrays, so, this ...
Mike Tango wrote:
int[][] multipliers = new int[10][] works

means declaring an array of array of ints. And instantiate the array of array of ints to 10 elements. Additionally, don't instantiate any of the elements, so they are all (10 elements) assigned a value of null.

And this ...
Mike Tango wrote:
int[][] multipliers = new int[][] doesn't work

means declaring an array of arrays of ints.... but there isn't enough information to instantiate. After all, what size are the arrays? For the first example above, you can leave the second value blank, which means to not instantiate the elements, but leaving the first value blank doesn't make sense. Do you want the array of array to be null? If so, then, you can do this instead...

Henry
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Tango wrote:
And how to read the nextline:
int[] quotes[] = ....

Java allows you to place the brackets on either side of the declaration. The one on the right of the variable applies to only the variable specified, while the one on the right of the type applies to all the variables. With only one variable declaration, it doesn't make a difference.

So... is the same as and also the same as Henry
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . but Henry's second example shows the best format, because it makes it clear that the variable is of type array of arrays.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!