views:

103

answers:

1

In the blackberry documentation, they say that an array takes one object handle:

int[] array; // 1 object handle

How many handles does an array of arrays take?

int[][] array = new int[4][2]; // how many handles?

I can't figure out if it would be a single one since, after all, the array can be construed as a single piece of memory or is it multiples (5 in this case) because there would be one per element in the first-level array?

+3  A: 

Java doesn't have multidimensional arrays, as you've found out. It has array of arrays.

Given this declaration:

int[][] array = new int[4][2];

There are 4 int[] arrays, each having 2 int elements.

Object o1 = array[0];
Object o2 = array[1];
Object o3 = array[2];
Object o4 = array[3];

Consequently, this means that array is also an Object[].

Object[] oX = array;

Just because you have an int[][], doesn't mean that each int[] is distinct, of course.

int[][] weird = new int[4][];
weird[0] = weird[1] = weird[2] = weird[3] = new int[5];

Now there is only one int[], and each weird[i] shares this reference.

weird[0][2] = 5;
System.out.println(weird[3][2]); // prints "5"
polygenelubricants
Sure, it's not five arrays (handles), namely array, array[0], array[1], array[2] and array[3]?
Andreas_D
@Andreas: Five arrays objects, yes. 4 `int[]`, one `Object[]`.
polygenelubricants