• 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
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

array question

 
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is a question in array:



How could the output is 11, not 9? I thought both x.length and x[i].length are 3. How would line 10 change the length?
Thanks.
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1)The code output is not single line output.The output is:
size = 1
size = 2
size = 3
size = 4
size = 2
size = 3
size = 3
size = 4
size = 5
size = 6
size = 7

2)Length of x and x[i] are not same
length of x = 3

length of x[0]=4,x[1]=2,x[2]=5

3)Line 3 changes the length ; it allocates objects.If that line is not used we wud get null pointer exception.

My Doubt:
If the code were like:
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);
}
}
I get the compiler error that i and j not initialized....
How come thats possible?
I and j wud have surely initialized in the for loop?
 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the compiler doesn't know that the outer loop body (the inner loop) executes so j (used in the println statement) may not be initialized. (The println statement is outside of both loops.)
 
reubin haz
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone explain more detail about this compiler problem? Thanks.
[ May 26, 2005: Message edited by: reubin nibuer ]
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that taking out the curly braces has reduced the scope of the loops, I believe that the scope has been reduced it to 1 line , as in an if statement.
at best I think the compiler sees this;

At worst it would see this;


At any rate you do't save anything by cutting out the braces the compiler puts them in for you.
R1885
 
Geethakrishna Srihari
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No...the compiler problem refers to the code which i pasted.
 
Allain Walker
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry thought that it was the other code.
 
Allain Walker
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is happening is that you are creating a new array of int that has i+j+1 members.
So when you return th esize of the last array by calling x[i][j].length it returns the number of members of the new int array.
try putting this in

and you will see the difference in the outputs.
Hope it helps
 
reply
    Bookmark Topic Watch Topic
  • New Topic