This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Cloud Application Architecture Patterns: Designing, Building, and Modernizing for the Cloud and have Kyle Brown, Bobby Woolf and Joseph Yodor on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Arrays doubt

 
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 62
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

What part of the explanation you didn�t understand?

Marcos.
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks krishnan if i have any doubts i will post again
 
reply
    Bookmark Topic Watch Topic
  • New Topic