Forums Register Login

multidimensional array declaration

+Pie Number of slices to send: Send

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?
1
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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 :



??
+Pie Number of slices to send: Send
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:
1
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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


reply
reply
This thread has been viewed 532 times.
Similar Threads
multidimensional array assignments
multidimensional array
MultiDimensional Arrays
Casting a multidimensional array.
Review Question about Multidimensional Array
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 23:20:45.