Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

array question  RSS feed

 
reubin haz
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • 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.
 
Geethakrishna Srihari
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • 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?
 
Vlado Zajac
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Can someone explain more detail about this compiler problem? Thanks.
[ May 26, 2005: Message edited by: reubin nibuer ]
 
Allain Walker
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
Sorry thought that it was the other code.
 
Allain Walker
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • 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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!