|
UBBFriend: Email This Page to Someone! |
JavaRanch
Big Moose Saloon Programmer Certification Three dim array
|
next newest topic | next oldest topic |
Author | Topic: Three dim array |
Java2learner greenhorn |
posted March 15, 2000 09:36 PM
Hi, This is a mock exam question:
code: Select valid answer: I tried the code and it gives ArrayIndexOutOfBoundsException I have modified the code to display all the values in the array:
code: The question I have is with this line of code code: Am I correct here? Can anybody explain this please? Thanks... IP: Logged |
Jim
Yingst sheriff |
posted March 15, 2000 11:03 PM
Look again:
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).] IP: Logged |
maha
anna bartender |
posted March 15, 2000 11:18 PM
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? regds [This message has been edited by maha anna (edited March 15, 2000).] IP: Logged |
Manju
Swamy greenhorn |
posted March 16, 2000 11:58 AM
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 code: 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).] IP: Logged |
Java2learner greenhorn |
posted March 16, 2000 04:54 PM
Thanks to all for your replies. IP: Logged |
All times are MT (US) |
next newest topic | next oldest topic |
|
Powered by: Ultimate Bulletin Board, Version 5.44a
© Infopop Corporation
(formerly Madrona Park, Inc.), 1998 -
2000.