If you click on this stuff and give your money to some city slickers, JavaRanch gets a kickback. Hoooooooo Doggy!     
Do not post questions from the actual certification exams. If you see a question from an actual exam, please e-mail the moderators privately to let us know. To discuss a question from a mock exam, please mention which mock exam and the question number (if provided) so we know it's not from the real exam.



UBBFriend: Email This Page to Someone!
  JavaRanch Big Moose Saloon
  Programmer Certification
  Three dim array

Post New Topic  Post A Reply
profile | register | preferences | faq | search

next newest topic | next oldest topic
Author Topic:   Three dim array
Java2learner
greenhorn
posted March 15, 2000 09:36 PM     Click Here to See the Profile for Java2learner     Edit/Delete Message Reply w/Quote
Hi,
This is a mock exam question:

code:
public class ThreeDimArray
{
public static void main(String args[])
{
String[][][] arr =
{
{ {}, null },
{ { "1", "2" }, { "1", null, "3" } },
{},
{ { "1", null } }
};
System.out.println( arr.length + arr[1][2].length );
}
}

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:

code:
public class ThreeDimArray
{
public static void main(String args[])
{
String[][][] arr =
{
{ {}, null },
{ { "1", "2" }, { "1", null, "3" } },
{},
{ { "1", null } }
};


// System.out.println(arr.length + arr[1][2].length ); // Prints ArrayIndexOutOfBoundsException

System.out.println(arr.length); // Prints 4

System.out.println( arr[0][0].length ); // Prints 0
// System.out.println( arr[0][0][0] ); //Prints ArrayIndexOutOfBoundsException
System.out.println( arr[0][0] ); // prints junk values
System.out.println( arr[0] ); // prints junk values
// System.out.println( arr[0][1].length ); //Prints NullPointerException - Why?? IMO - length should be 0
System.out.println( arr[0][1] ); // prints null

System.out.println( arr[1][0].length ); // Prints 2
System.out.println( arr[1][0][0] ); // Prints 1
System.out.println( arr[1][0][1] ); // Prints 2

System.out.println( arr[1][1].length ); // Prints 3
System.out.println( arr[1][1][0] ); // Prints 1
System.out.println( arr[1][1][1] ); // Prints null
System.out.println( arr[1][1][2] ); // Prints 3

System.out.println( arr[2].length ); // Prints ArrayIndexOutOfBoundsException - Why? IMO - length should be 0

System.out.println( arr[3][0].length ); // Prints 2
System.out.println( arr[3][0][0] ); // Prints 1
System.out.println( arr[3][0][1] ); // Prints null

}
}


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,

code:
String arr1 = {};
System.out.println( arr.length ); // Prints length as 0

Am I correct here? Can anybody explain this please?

Thanks...

IP: Logged

Jim Yingst
sheriff
posted March 15, 2000 11:03 PM     Click Here to See the Profile for Jim Yingst   Click Here to Email Jim Yingst     Edit/Delete Message Reply w/Quote
Look again:

    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]
}
}
};

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     Click Here to See the Profile for maha anna   Click Here to Email maha anna     Edit/Delete Message Reply w/Quote
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).]

IP: Logged

Manju Swamy
greenhorn
posted March 16, 2000 11:58 AM     Click Here to See the Profile for Manju Swamy     Edit/Delete Message Reply w/Quote
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.

code:

public class ThreeDimArray {

public static void printArray(String[][][] myArray) {
int i = 0, j = 0, k =0;
try {
System.out.println("length:" + myArray.length + " value:" + myArray);
for (i = 0; i < myArray.length; i++) {
System.out.println("i:" + i + " length[i]:" + myArray[i].length + " value[i]:" + myArray[i]);
if (myArray[i].length == 0) continue;
try {
for (j = 0; j < myArray[i].length; j++) {
System.out.println("i:" + i + " j:" + j + " length[i][j]:" + myArray[i][j].length + " value[i][j]:" + myArray[i][j]);
if (myArray[i][j].length == 0) continue;

try {
for (k = 0; k < myArray[i][j].length; k++) {
System.out.println("i:" + i + " j:" + j + " k:" + k + " value[i][j][k]:" + myArray[i][j][k]);
if (myArray[i][j].length == 0) continue;
System.out.println("Array [" + i + "][" + j + "][" + k + "] = " + myArray[i][j][k]);
}
} catch (NullPointerException npe) {
System.out.println("Array [" + i + "][" + j + "][" + k + "] = " + null);
continue;
}
}
} catch (NullPointerException npe) {
System.out.println("Array [" + i + "][" + j + "] = " + null);
continue;
}
}
} catch (NullPointerException npe) {
System.out.println("Array [" + i + "] = " + null);
}
}

public static void main(String args[]) {
String[][][] arr = {
{ {}, null },
{ { "1", "2" }, { "1", null, "3" } },
{},
{ { "1", null } }
};

printArray(arr);
}
}


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     Click Here to See the Profile for Java2learner     Edit/Delete Message Reply w/Quote
Thanks to all for your replies.

IP: Logged

All times are MT (US)

next newest topic | next oldest topic

Administrative Options: Close Topic | Archive/Move | Delete Topic
Post New Topic  Post A Reply
Hop to:

Contact Us | JavaRanch

Powered by: Ultimate Bulletin Board, Version 5.44a
© Infopop Corporation (formerly Madrona Park, Inc.), 1998 - 2000.