Win a copy of Functional Reactive Programming this week in the Other Languages forum!

# K&B , Chap1 Q11

Ram Han
Ranch Hand
Posts: 49
Below question is from K&B book Chap 1, Q11
Given the following,
1. public class Test {
2. public static void main(String [] args) {
3. int [] [] [] x = new int [3] [] [];
4. int i,j;
5. x[0] = new int[4][];
6. x[1] = new int[2][];
7. x[2] = new int[5][];
8. for (i=0; i<x.length; i++)
9. for (j=0; j<x[i].length; j++) {
10. x[i][j] = new int [i + j + 1];
11. System.out.println("size = " + x[i][j].length);
12. }
13.

how many lines of output will be produced? (Choose one.)
A. 7
B. 9
C. 11
D. 13
E. Compilation fails
F. An exception is thrown at runtime

Author nicely escapes without giving a explanation on this question

Can someone explain , how it works

Saif Ullah
Greenhorn
Posts: 29
I haven't run this example, but the loops sound to work like that. The loop with variable i, (outer loop) runs for three iterations as the first dimension of x array is 3, for each iteration, the inner loop iterates for 4, 2 and 5 times so total iterations are 11. I hope you can understand how inner loop is iterated for each individual size mentioned for 2nd dimension of x-array.

Bert Bates
author
Sheriff
Posts: 8900
5
Awww,

Author nicely escapes without giving a explanation on this question

I thought our "explanation" was pretty useful

Bert

Keith Lynn
Ranch Hand
Posts: 2409
2. public static void main(String [] args) {
3. int [] [] [] x = new int [3] [] [];
// Here an array of size 3 is created. Each element in the array is an
array which contains an array of ints.

4. int i,j;
5. x[0] = new int[4][];
// The first entry in the array referred to by x is set to an array containing 4 int arrays.

6. x[1] = new int[2][];
// The second entry in the array referred to by x is set to an array containing 2 int arrays.

7. x[2] = new int[5][];
// The third entry in the array referred to by x is set to an array containing 5 int arrays.

8. for (i=0; i<x.length; i++)
9. for (j=0; j<x[i].length; j++) {
10. x[i][j] = new int [i + j + 1];
11. System.out.println("size = " + x[i][j].length);
12. }
13.

// The outer loop here executes 3 times. On the first pass, a loop of length 4 is run. On the second pass, a loop of length 2 is run. On the third pass, a loop of length 5 is run.

So you have 11 runs. Each time you print a line.

sumitk das
Greenhorn
Posts: 4
Hi ,

MuraliRam Narasimhan has mentioned the name of the book as k&b , is it
khalid mughal's book for scjp. I am a new one in this forum, so pls let
me know.

Thanks,
Sumit

Rajasekar Elango
Ranch Hand
Posts: 105
Its certification book by Kathy Sierra, Bert Bates

Niranjan Deshpande
Ranch Hand
Posts: 1277
hi...the only way to solve questions "involving dynamic initialization of arrays using fo loops" is to dray a picture of the array. its a piece of cake then

bye

Ranch Hand
Posts: 175
Keith, your explanation is good, but for a minor (major?) detail.
Each element in the x int array refers to an array of ints and not an array of int arrays, which would make x a 3D array.
Sashi

Keith Lynn
Ranch Hand
Posts: 2409
But isn't it the case that

int[] x -> x refers to an array of ints

int[][] x -> x refers to an array of arrays of ints

int[][][] x -> x refers to an array of arrays of arrays of ints

Junilu Lacar
Bartender
Posts: 7766
62
Sashi, I concur with Keith:

The original declaration int[][][] x means that each element of the array x is an int[][].

Ranch Hand
Posts: 175
Well, I came, I saw, I concurred.
Sashi

Gyan Shankar
Ranch Hand
Posts: 65
Number of lines of o/p = 4+2+5 = 11

Jim Yingst
Wanderer
Sheriff
Posts: 18671
[Sashi]: Well, I came, I saw, I concurred