• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Can anyone explain this code from ...Java 2 Part-1 the programmers exam - page 49

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;i<x[i].length;j++) {

x[i][j] = new int[i+j+1];
System.out.println("size = " +x[i][j].length);
}
}
}
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check ur code...
In inner for loop, is it i<x[i].length or j<x[i].length
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are a lot of things happening here. What exactly is your question about this code?
 
uday atyam
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
opps...I am sorry...the inner loop is "j<x[i].length".

Can you tell me what the output is ...
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just compile and run it.
//i = 0; x.length = 3; x[i].length = 4
size = 1
size = 2
size = 3
size = 4
//i = 1; x.length = 3; x[i].length = 2
size = 2
size = 3
//i = 2; x.length = 3; x[i].length = 5
size = 3
size = 4
size = 5
size = 6
size = 7
[ May 16, 2006: Message edited by: wise owen ]
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic