Hi Kavita,
In Line 2:
A13[][] a2 = new A13[2][1]; // 2
a2 is an array of arrays of A13 objects. It contains two elements a2[0] and a2[1]. Both a2[0] and a2[1] are arrays of A13 objects and each contain only one element. In the case of a2[0], the element is a2[0][0]. In the case of a2[1], it is a2[1][0].
In line 8:
a3 is an array of arrays of arrays of A13 objects. The dimensions are 3 x 3 x 3, in the beginning. But later, the dimensions change to 3 x 2 x 1, due to reassignments of the top level elements. When dereferencing with a3[2][2][2], the index 2 is trying to access the 3rd element (
Java arrays are 0-based). There is no 3rd element for the second and third level indices, as we said the array gets changed to 3 x 2 x 1. This causes an IndexOutOfBoundsException, as Barry explained very well.
Next problem:
A11[][] a2 = new A11[2][]; // 2
a2 is an array of arrays of A11 objects. It has two elements a2[0] and a2[1]. Both are assigned to null because the second dimension was not specified. Later on, in line 5, the nulls are overwritten with a1, which is an array of A11 objects, containing only one element.
System.out.print(a3[2][1][0]); // 7
a3 is an array of arrays of arrays of A11 objects. It has 3 elements, each is an array of arrays of A11 objects. These are all set to null in line 3. Later, in line 6, all three of these elements( a3[0], a3[1], a3[2] ) are assigned to a2, which I just described above.
In the end, the three elements of the A11[][][] array all refer to the same object. This object is a A11[][] array with two elements. Both elements of the A11[][] array refer to the same object. This is an A11[] array with a single element. This single element is an A11 object (created in line 4). This means that every (lowest-level) A11 element in the A11[][][] array is actually the same A11 object.
It helps to draw diagrams when figuring this stuff out. Look at page 31 in Sierra & Bates
SCJP 1.4 Study Guide, for example. Visualizing the problem makes it a lot easier to understand.
I hope this helps.
[ July 11, 2006: Message edited by: Douglas Chorpita ]