• 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

K&B , Chap1 Q11

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


Answer C.


Author nicely escapes without giving a explanation on this question

Can someone explain , how it works
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Awww,

Author nicely escapes without giving a explanation on this question



I thought our "explanation" was pretty useful



Bert
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its certification book by Kathy Sierra, Bert Bates
 
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 17652
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sashi, I concur with Keith:

The original declaration int[][][] x means that each element of the array x is an int[][].
 
Sasikanth Malladi
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I came, I saw, I concurred.
Sashi
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Number of lines of o/p = 4+2+5 = 11
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Sashi]: Well, I came, I saw, I concurred

reply
    Bookmark Topic Watch Topic
  • New Topic