• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

multi-dimension array question - dan's exam

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this question, i do understand how line //4 prints null, but the answer mentions that line //7 generates a run time error.
The array a3 has 3*3*3=27 number of elements. so in line //7 the array referenced by a2 is assigned to array assigned by a3[0], a3[1] and a3[2]. this is where i have the doubt, won't stmt //8 print the value null??
could someone clarify what the lines //6 through //8 mean?
Thanks in advance

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
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Line 7 does not cause a runtime exception but line 8 does. Take a close look at line 7 and see what happens:

a3[x] dereferences a two-dimensional array, that's why line 7 is legal otherwise you would get a compiler error. So after the assignment at line seven, a3 now only has 6 elements or A13[3][2][1], because all three of the primary sub-arrays of a3 (a3[0], a3[1], a3[2]) are now referencing a2 which is defined as A13[2][1]. Modifying the code like this may make it a littler clearer:
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi sandyah.

means both a2[0] and a2[1] are assigned a one-dimesion array holding a null A13. code still works fine here.

means each of the three base arrays of a3 were assigned a 2-dimensional array [2][1]. with the given dimension, you have 2 base arrays each holding a single array that contains a null A13. remember, the only valid indexes for an array with the dimension [2][1] are [0][0], [1][0]. so far, so good.

the problem falls really in line 8 because a3[2] has been assigned to hold arrays with the dimension [2][1] as discussed above. again, the only valid indexes for a3[2] will then will be a3[2][0][0] and a3[2][1][0].
therefore, trying to access the index a3[2][2][2] which is out of range will give you an ArrayIndexOutOfBoundsException.
the key really is to understand that in java, multidimensional arrays are simply arrays of arrays. hope this helps.
[ March 14, 2004: Message edited by: nikki lorenzo ]
[ March 14, 2004: Message edited by: nikki lorenzo ]
 
Sandhya Harish
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you so much, Michael and nikki, it has become much clearer now.
i guess the part where i really got confused was when a2 was assigned a3 elements in line 7. now after actually seeing what has been assigned to the elements of a3, i know that only 6 elements are present and therefore anything beyond that is an ArrayIndexOutofBoundsException.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic