• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Three dim array

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
This is a mock exam question:

Select valid answer:
a. ArrayIndexOutOfBoundsException //ans
b. NullPointerException
c. 4
d. 7
I tried the code and it gives ArrayIndexOutOfBoundsException
I have modified the code to display all the values in the array:

The question I have is with this line of code
System.out.println( arr[0][1].length ); //Prints NullPointerException - Why?? IMO - length should be 1, since arr[0][1][0] contains null value.
Also,
in the line: System.out.println( arr[2].length ); // Prints ArrayIndexOutOfBoundsException - Why? IMO - length should be 0, because,

Am I correct here? Can anybody explain this please?
Thanks...
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look again:
<code><pre> String[][][] arr =
{ // arr[0]
{ // arr[0]
{}, // arr[0][0]
null // arr[0][1]
},
{ // arr[1]
{ // arr[1][0]
"1", // arr[1][0][0]
"2" // arr[1][1][1]
},
{ // arr[1][1]
"1", // arr[1][1][0]
null, // arr[1][1][1]
"3" // arr[1][1][2]
}
},
{}, // arr[2]
{ // arr[3]
{ // arr[3][0]
"1", // arr[3][0][0]
null // arr[3][0][1]
}
}
};</pre></code>
The red null corresponds to arr[0][1]. At this point in the array, the third dimension does not exist, because there's no array there to hold it in. Note that there are no extra parentheses around the null which would justify indenting it one more level.
As for your second question: when I run it, the line prints 0 as expected. I think you're mistaken.

[This message has been edited by Jim Yingst (edited March 15, 2000).]
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
System.out.println( arr[0][1].length ); //Prints NullPointerException - Why??
Since arr[1] must be a ref to another array , and the ref arr[0][1] is set to null means, we have not allocated an array which has to be pointed from this reference arr[0][1]. So attempting to calculate the length of an array which has not yet been allocated memory, because the ref is null here, generates 'NullPointerException'.
System.out.println( arr[2].length ); // Prints ArrayIndexOutOfBoundsException - Why?
No length of arr[2] is zero . 0 is printed. No exception will be thrown. Please check again.
Since here arr[2] must be an array of arrays, and this code sets arr[2] to just an empty array, which does not have any arrays inside , trying to calculate the length of an empty array prints 0.
regds
maha anna

[This message has been edited by maha anna (edited March 15, 2000).]
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the code to print the array. Any time you see values similar to this
[[[Ljava.lang.String;@f35792c0 (it is not garbage), it is the String representation of the Object
(Arrays are objects in Java). All objects are inherited from Object class. The Object
class has toString() method. When you try to print the object in the
System.out.println() method, it will explicitly call object's toString() method.
The reason for loops are enclosed in the nested try...catch block is to catch the NullPointerException
and print the null values.

Go thorough the program output and analyze. Let us know if you need any further clarifications.

[This message has been edited by Manju Swamy (edited March 16, 2000).]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all for your replies.
 
Do you want ants? Because that's how you get ants. And a tiny ads:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic