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?
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.
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 :
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:
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.
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.
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 by:autobot
Fire me boy! Cool, soothing, shameless self promotion:
a bit of art, as a gift, that will fit in a stocking