Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

multidimensional array declaration  RSS feed

 
Kamila Bertran
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

OK, lets say I want to declare a multidimensional array. I found on the net somewhat interesting (because I don't fully understand how it works) declaration and initialization:



ufff... Ok, this bit is easy. But than something like this happens:



So what does it actually mean? That my array3D is a 3-dimentional array where each element is a 2 dimensional array? Than I initialize two first elements and leave the third one empty?
 
Stephan van Hulst
Saloon Keeper
Posts: 7817
142
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The mistake is in thinking that arrays are multidimensional. Java doesn't have arrays with more than one dimension.

int[] is an array of ints.
int[][][] is an array of arrays of arrays of ints.

That means that the result of array3D[0] is an int[][], and the result of array3D[0][0] is an int[].

The distinction is important, because multidimensional arrays in other languages are just contiguous blocks of memory for the base type. In those languages you wouldn't be able to do array3D[0][0]; you always have to specify all coordinates: array3D[0,0,0]. Note that the syntax is also different.
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can declare and initialise an array of arrays like this:-Despite my calling it matrix, it isn't really a matrix at all. You can print it with
 
Kamila Bertran
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's was good but I think I need to give it a minute to sink in.

So I have an array [] [] [] here, where every element is an array.
this bit defines how many of those 'elements' are inside.
so i have array[0] that has two more arrays inside, the same goes for array[1]&array[2].
o than if I wanted to initialize the whole thing I would have to do something like :



??
 
Stephan van Hulst
Saloon Keeper
Posts: 7817
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can do it in any way you like. The problem with the approach you describe, is that if you edit array[0][0][0], that will also edit the value of array[0][1][0], array[1][0][0] and array[1][1][0], because they all refer to the same array.

To initialize nested arrays, I prefer to use nested loops:
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kamila Bertran wrote:
So I have an array [] [] [] here, where every element is an array.
this bit defines how many of those 'elements' are inside.


No. As mentioned, Java does not directly support multi-dimensional arrays. The array variable is simply an array of references -- which each element just happens to reference an array of int arrays.

This is no "inside", which implies a storage of an multi-dimensional array. It is simply an array of references.

Henry
 
Kamila Bertran
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:

No. As mentioned, Java does not directly support multi-dimensional arrays. The array variable is simply an array of references -- which each element just happens to reference an array of int arrays.

This is no "inside", which implies a storage of an multi-dimensional array. It is simply an array of references.

Henry


Got it. Thanks. That makes it so much easier to think of it.
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kamila Bertran wrote:. . . if I wanted to initialize the whole thing I would have to do something like : . . .
As Stephan said earlier, there are all sorts of ways to populate that array. Probably too many to list. I showed you one way to do it (but only with two pairs of []), and Stephan another. You can use your method, but that would require you to initialise the top‑level array with sizes first. My technique has the advantage that it automatically creates arrays of different sizes (sometimes called jagged arrays).

Whatever you do, be sure to fill all the arrays, otherwise you will have nulls, and, as you doubtless already know, nulls are trouble.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!