wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Arrays doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Arrays doubt" Watch "Arrays doubt" New topic
Author

Arrays doubt

Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Source anchisholm

Hi All,
Hey i am not understanding the explanation given to the answer of this question. Can anybody explain it?

class A13 {}
class A14 {
public static void main(String[] arg) {
A13[] a1 = new A13[1]; // 1
A13[][] a2 = new A13[2][1]; // 2
A13[][][] a3 = new A13[3][3][3]; // 3
System.out.print(a3[2][2][2]); // 4
a1[0] = new A13(); // 5
a2[0] = a2[1] = a1; // 6
a3[0] = a3[1] = a3[2] = a2; // 7
System.out.print(a3[2][2][2]); // 8
}}

What is the result of attempting to compile and run the program?

a. Prints: null
b. Prints: nullnull
c. Compile-time error at 1.
d. Compile-time error at 2.
e. Compile-time error at 3.
f. Compile-time error at 4.
g. Compile-time error at 5.
h. Compile-time error at 6.
i. Compile-time error at 7.
j. Compile-time error at 8.
k. Run-time error


Output: null and Run-time error

explanation : The declaration A13[] a1 = new A13[1] declares a variable a1 that references an array that contains one component of type A13. The declaration A13[][] a2 = new A13[2][1] declares a variable a2 that references an array that contains two components of type A13[]. In other words, the array referenced by a2 contains two references to two subarrays of type A13[]. The size of each subarray is 1. The declaration A13[][][] a3 = new A13[3][3][3] declares a variable a3 that references an array that contains three components of type A13[][]. In other words, the array referenced by a3 contains three references to three subarrays of type A13[][]. The dimensions of the subarrays are 3 X 3. The dimensions of the array referenced by a3 are 3 X 3 X 3. The number of elements in the array referenced by a3 is 3 * 3 * 3 = 27 elements. Each of the three subarrays contains three components, where each is a reference to a subarray of type A13[]. Each of the 27 elements of the array referenced by a3 is a reference of type A13 that has been initialized to the default value of null. At line 7, a reference to the array referenced by a2 is assigned to each of the three components of the array referenced by a3, a3[0] = a3[1] = a3[2] = a2. Since the array referenced by a2 has dimensions 2 X 1, the array referenced by a3 now has dimensions 3 X 2 X 1. The number of elements is 6. An attempt to access any element beyond those 6 results in an exception at run-time.
Marcos R Oliveira
Ranch Hand

Joined: Apr 20, 2002
Posts: 51
Hi,

What part of the explanation you didn�t understand?

Marcos.


SCJP 1.4 - SCJP 1.6 - SCWCD in progress
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Hi the below line i did not understand

At line 7, a reference to the array referenced by a2 is assigned to each of the three components of the array referenced by a3, a3[0] = a3[1] = a3[2] = a2. Since the array referenced by a2 has dimensions 2 X 1, the array referenced by a3 now has dimensions 3 X 2 X 1. The number of elements is 6. An attempt to access any element beyond those 6 results in an exception at run-time.
Meena R. Krishnan
Ranch Hand

Joined: Aug 13, 2006
Posts: 178
If your question is why runtime error?, then look at the last few lines of your explanantion.


At line 7, a reference to the array referenced by a2 is assigned to each of the three components of the array referenced by a3, a3[0] = a3[1] = a3[2] = a2. Since the array referenced by a2 has dimensions 2 X 1, the array referenced by a3 now has dimensions 3 X 2 X 1. The number of elements is 6. An attempt to access any element beyond those 6 results in an exception at run-time.



a3[0] = a3[1] = a3[2] = a2; // 7
At line 7, all the three elements of a3 are pointing to a2 which is a 2D array of dimension 2x1.

System.out.print(a3[2][2][2]); //8
At line 8, you're trying to print the third element of a3, again, which has a dimension of 2x1.
ie, only the following are valid: Array indexes are zero based.
a3[2][0][0]
a3[2][1][0]

Therefore, trying to access an out of bounds index @ 2,2,2 is causing runtime exception.

[ April 30, 2007: Message edited by: M Krishnan ]
[ April 30, 2007: Message edited by: M Krishnan ]
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Thanks krishnan if i have any doubts i will post again
 
 
subject: Arrays doubt
 
Similar Threads
I got a question about [ ] [ ] [ ].
Question from Dan test
Array
help in array
multi-dimension array question - dan's exam