Please use tags to preserve formatting, if any, in code. If you get a runtime exception tell us what it is.
a3 references an array a2 with two elements each of which are references to an array with one element. You are referencing the 3rd argument of a two element array, so you are getting an IndexOutOfBoundsException.
a2 is an array of arrays of A13 objects. It contains two elements a2 and a2. Both a2 and a2 are arrays of A13 objects and each contain only one element. In the case of a2, the element is a2. In the case of a2, it is a2.
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, 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.
A11 a2 = new A11; // 2
a2 is an array of arrays of A11 objects. It has two elements a2 and a2. 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); // 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, a3, a3 ) 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 ]
SCJP 1.4 - 95%
posted 14 years ago
Thanx Douglas Chorpita i understood it..whatever u explain.. now i will draw the diagram..accorading to katthy's book..so that it clears my concept very perfecly... Thanx for cheering
We're all out of roofs. But we still have tiny ads: