Win a copy of Head First Android this week in the Android forum!
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
• Paul Clapham
• Ron McLeod
• Tim Cooke
• Junilu Lacar
Sheriffs:
• Rob Spoor
• Devaka Cooray
• Jeanne Boyarsky
Saloon Keepers:
• Jesse Silverman
• Stephan van Hulst
• Tim Moores
• Carey Brown
• Tim Holloway
Bartenders:
• Jj Roberts
• Al Hobbs
• Piet Souris

# let me know about the logic here

Greenhorn
Posts: 14
• Number of slices to send:
Optional 'thank-you' note:
This is from K&B chapter 1 language fundamentals...Please let me know about the logic here...
public class test
{
public static void main(String []args)
{
int [][][] x = new int[3][][];
int i,j;
x[0] = new int[4][];
x[1] = new int[2][];
x[2] = new int[5][];
for(i- 0;i<x.length;i++)
for(j=0;j<x[i].length;j++)
{
x[i][j] = new int[i+j+1];
System.out.println("size ="+x[i][j].length);
}
}
}

thanks

Ranch Hand
Posts: 143
• Number of slices to send:
Optional 'thank-you' note:
Hi Surekha,

Are you sure you got the answer 11? When I tried this question I got something different.

Asha

Greenhorn
Posts: 8
• Number of slices to send:
Optional 'thank-you' note:
actuallly x is a 3 dimensional array.
first you declare x[0],x[1], and x[2] as three 2 -D arrays .
then x[i][j] = new int[i+j+1];(will point to a 1-D array),
initializes a single dimensional array of length i+j+1 and this length
is being printed.

Ranch Hand
Posts: 62
• Number of slices to send:
Optional 'thank-you' note:
Hi,

Actually the question is how many lines of output will be produced ?

Explaination goes as follows

length of x, a 3d array is 3 and x[0] has the length 4, x[1] has 2 and x[2] has 5 from the declarations.

When we say for(i=0;i<x.length;i++) leads to for(i=0;i<3;i++) --> outer for loop.
for the first iteration for outer for loop , inner for loop leads to
for(j=0;j<4;j++) cos for 1st iteration i =0 and x[i] =x[0] has the length 4.
hence the number of lines of output for 1st iteration of outer for loop is 4.

similarly we can say
when i =0 -- > number of lines of output is 4
when i =1 -- > number of lines of output is 2
when i =2 -- > number of lines of output is 5

Hence total number of lines of output will be 4+2+5 = 11.

hope this clears the doubt.

Suchitra

surekha raju
Greenhorn
Posts: 14
• Number of slices to send:
Optional 'thank-you' note:
The way u explained is excellent suchitra...
Thax a lot

Ranch Hand
Posts: 176
• Number of slices to send:
Optional 'thank-you' note:
hi sureka,

If we assume in the outer loop it is i=0 not i-0, then we will get 11 different lines of output not i think 11 value as output.

what do you say

surekha raju
Greenhorn
Posts: 14
• Number of slices to send:
Optional 'thank-you' note:
hey guys really sorry in the for loop i=0 and not i-0

 Consider Paul's rocket mass heater.