Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!

# Constructing multidimensional arrays

leo donahue
Ranch Hand
Posts: 327
If I have:
int[][] myArray = new int[3][];
Is it right to say that I have two one-dimensional arrays where the first array holds 3 int references. What about the second one-dimensional array? Is it just unspecified? What is it's length?

Michael Morris
Ranch Hand
Posts: 3451
Is it right to say that I have two one-dimensional arrays where the first array holds 3 int references. What about the second one-dimensional array? Is it just unspecified? What is it's length?
What you actually have is an array of array of ints. Confused yet? It' really not that tough. Take a look at this:

myArray[0-2] are single dimensional arrays of int. As you can see in the above code, they don't have to have the same number of elements.

leo donahue
Ranch Hand
Posts: 327

So the empty bracket after new int[3][] lets me assign any lengths I want to the array of array of ints?
If I changed the code to new int[3][3] what does this do?
Does this mean I can't then do:
myArray[2] = new int[4];
[ May 15, 2003: Message edited by: leo donahue ]

Michael Morris
Ranch Hand
Posts: 3451
If I changed the code to new int[3][3] what does this do?
myArray[2] = new int[4];
No, that's perfectly legal, you just assigned a new array to element 2. Look at this:

Try the code as is and then uncomment the first myArray[2][3] = 1; and see what happens.
Remember that arrays are objects so just as you can reassign a reference to a different object you can reassign an array reference to a different array object.
[ May 15, 2003: Message edited by: Michael Morris ]

jim gotti
Ranch Hand
Posts: 36
So a 2D array can more or less be viewed as a Matrix?
where myArray [2][3] refers to the 2nd row, 3rd column of the 2d array?

Michael Morris
Ranch Hand
Posts: 3451

So a 2D array can more or less be viewed as a Matrix?
where myArray [2][3] refers to the 2nd row, 3rd column of the 2d array?

You bet. But unlike a matrix, which is always rectangular, a 2D array in java can be assymetrical, the member arrays are not required to have the same number of elements.

leo donahue
Ranch Hand
Posts: 327
I still don't get it. Look at the following:

On line # 3, what is happening? Do I have an array of 3 int arrays each with a length of 3?
On line # 5, what is happening? Why myArray[1]? What does this array index do for me? If I change it from 1 to 2, nothing is different about the print out.
Am I just assigning a new int array with a length of 3 at myArray array index of 1?
[ May 16, 2003: Message edited by: leo donahue ]

Ranch Hand
Posts: 1873
hi Michael,
so actually there is no meaning of having the second 3 in int a[][] = new int[3][3] right?
what does that second 3 do anyways?
regards
maulin

Joel McNary
Bartender
Posts: 1840
so actually there is no meaning of having the second 3 in int a[][] = new int[3][3] right?
what does that second 3 do anyways?

The second three provides initialization. If you said:
int[][] rupert = new int[3][];
you would get a 3-long array of int[]s where each element of the array is NULL. Remember, arrays are objects, and object arrays are initialized to null. If you then tried:
System.out.println(rupert[0][0])
you would get a null pointer exception.
Howerver, if you first said
int[][] rupert = new int[3][5];
you would get a 3-long array of int[]s where each element was initialized to a 5-long array of ints (not int[]s). These are primitive arrays, so the contents are initialized to 0. A call to
System.out.println(rupert[0][0])
would print out "0".

Ranch Hand
Posts: 1873
alright,
my conclusion is-
assuming we have infinite (a lot of) memory we can have N*Integer.MAX_VALUE number of ints stored in a 2D array if the declaration is like,
int[][] a = new int[N][X];
regardless of "X" here. correct???
regards
maulin

Michael Morris
Ranch Hand
Posts: 3451
On line # 3, what is happening? Do I have an array of 3 int arrays each with a length of 3?

Yes. You have 3 int[] arrays each of which contains 3 elements.
On line # 5, what is happening? Why myArray[1]? What does this array index do for me? If I change it from 1 to 2, nothing is different about the print out.
Remember that myArray is a reference to a two dimensional array and that two dimensional arrays can be thought of as an array of arrays. So when you say myArray[1] you are now referring to the second int array. As you say, the print out is not affected by line #5 because arrays of primitives are initialized to 0. One might surmize that nothing happened on line # 5, but something did happen. On line # 3 the second int array was created and myArray[1] referred to it; on line # 5 you created a new array and assigned myArray[1] to that one. So what happened to the array from line #3? It became orphaned and now the garbage collector will eventually carry it off to the trash pile.
Try this:

Ranch Hand
Posts: 1873
to generalize this...
the last index size doesn't matter.
if we had int[][][][][] a= new int[3][4][10][8][98];
then last [98] doesn't matter at all.
only first four index size will follow constraints.
ie. we can't write,
a[3][0][0][0] = new int[1];
as 3 will throw IndexOutOfBound...
try this...

here a[3][0][0][0]'s line throws exception but if u comment that, it would work...
regards
maulin

Joel McNary
Bartender
Posts: 1840
Originally posted by leo donahue:
I still don't get it. Look at the following:
On line # 3, what is happening? Do I have an array of 3 int arrays each with a length of 3?
On line # 5, what is happening? Why myArray[1]? What does this array index do for me? If I change it from 1 to 2, nothing is different about the print out.

1). Yes, you have a 3-long array of int[]s, each of which is a 3-long array of ints.
2). This is redundant code. It replaces the second int[] in the int[][] with a new, 3-long int[]. Since this is done before any manipulation of the int[][] (line 7), it has no effect whatsoever.

Ranch Hand
Posts: 1873
hi Joel,
i understand ur post. makes sense.
when i said the last index size doesn't matter i mean - we can assign any array size to the last index array if we do new int[] to the last bucket u know...
but the last index plays role in initialization as u mentioned...
thanks for making this clear to me.
regards
maulin

Joel McNary
Bartender
Posts: 1840
to generalize this...
the last index size doesn't matter.
if we had int[][][][][] a= new int[3][4][10][8][98];
then last [98] doesn't matter at all.
only first four index size will follow constraints.
ie. we can't write,
a[3][0][0][0] = new int[1];
as 3 will throw IndexOutOfBound...
try this...

here a[3][0][0][0]'s line throws exception but if u comment that, it would work...
regards
maulin

I think that either you are minsunderstanding what is goin on here, or I am misunderstanding your assertion.
None of the array initializers truly matter. You could say:
int[][][][][] a= new int[3][][][][];
and that would work just fine; you just can't access anything beyoud the top level because it hasn't been initialized.
if you said
int[][][][][] a = new int[3][4][5][10][98];
a[2][3][4][9] = new int[100];
that would be fine too; all the int[]s would be 98-long arrays except for the very last one in your multiplex, which is a 100-long array.
You get the ArrayIndexOutOfBounds becase the top-level array only has three elements: 0, 1, and 2. a[3] is trying to access the fourth element, so you get the AIOB.

leo donahue
Ranch Hand
Posts: 327
Originally posted by Michael Morris:
Remember that myArray is a reference to a two dimensional array and that two dimensional arrays can be thought of as an array of arrays. So when you say myArray[1] you are now referring to the second int array. As you say, the print out is not affected by line #5 because arrays of primitives are initialized to 0. One might surmize that nothing happened on line # 5, but something did happen. On line # 3 the second int array was created and myArray[1] referred to it; on line # 5 you created a new array and assigned myArray[1] to that one. So what happened to the array from line #3? It became orphaned and now the garbage collector will eventually carry it off to the trash pile.
Try this:
[/QB]

Michael,
I'm not sure why your printout prints myArray[1], then myArray[0], then myArray[2].
Anyway, here is my code again:

my dos window looks like this after running the code:
123
000
000
001
but if i change the line which reads:
myArray[1] = new int[3];
to
myArray[2] = new int[3];
my output becomes:
123
000
123
001
I am not following the pattern of what is happening when I re-initialize the myArray variable. I have the Kathy / Bert book as well as the Mughal and Rasmussen book in front of me trying to understand multidimensional arrays. It's just not sinking in.

Ranch Hand
Posts: 1873
hi leo,
well, i seem to confuse u. u r correct in what u say but what i mean was,
"IF" we specify index limits for sub dimensions then they matter upto "last-1" level u know...
so, if v write int[][][] a= new int[3][][];
thats valid and wouldn't constraint 2nd and 3rd dimension to any limit BUT if we write,
int[][][] a = new int[3][5][10];
then it would constraint 2nd dimension to have upto 5 elements ie. v can't write a[2][5] as it will be outofbound..
hope v r in synch..
regards
maulin

Joel McNary
Bartender
Posts: 1840
Originally posted by leo donahue:

I am not following the pattern of what is happening when I re-initialize the myArray variable.

I think I understand your confusion now. If you have a 2-Dimensional int array arr, saying arr[1] = new int[4] is not adjusting the size of the array, it is creating a new int[] and placing it as the second element of the array.
(When I visualize a n-dimensional array, I don't think in terms of rows and columns of boxes, I tend to think of them as boxes inside of other boxes. So when you say arr[1] = new int[4], You go the the second box of the main array, dump out the contents, and place a row of four boxes inside of that box.)
Consider (I'll use row-and-column terminology here, since that's how its graphically represented above...):
Case 1)
a. Create a 3x3 array (each element in the multiplex is initialized to 0)
b. Assign the second row to be {1, 2, 3)
c. Print the second row.
d. Assign the second row to a new array (initialized to {0, 0, 0}
e. Assign the value of 1 to the last element of the last row.
d. Print the entire 3x3 array.

Case 2)
a. Create a 3x3 array (each element in the multiplex is initialized to 0)
b. Assign the second row to be {1, 2, 3)
c. Print the second row.
d. Assign the third row to a new array (initialized to {0, 0, 0}
e. Assign the value of 1 to the last element of the last row.
d. Print the entire 3x3 array.
Note that the only difference between the two is step d.
In case 1 you are dumping out the contents of the second box (which originally contained three boxes which contained 1, 2, and 3) and replacing them with boxes that contain 0, 0, and 0 respectively.
In case 2 you are dumping out the contents of the thrird box (which originally contained three boxes which contained 0, 0, and 0) and replacing them with boxes that contain 0, 0, and 0 respectively.
[ May 16, 2003: Message edited by: Joel McNary ]

Michael Morris
Ranch Hand
Posts: 3451
See if this clears it up leo:

This shows that 2D arrays are stored in row major order. The comments show what should be contained in each row.