This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes array question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "array question" Watch "array question" New topic
Author

array question

reubin haz
Ranch Hand

Joined: May 12, 2005
Posts: 287
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.


SCJA, SCJP5.0, SCBCD, SCWCD
Geethakrishna Srihari
Ranch Hand

Joined: May 25, 2005
Posts: 30
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

Joined: Aug 03, 2004
Posts: 245
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

Joined: May 12, 2005
Posts: 287
Can someone explain more detail about this compiler problem? Thanks.
[ May 26, 2005: Message edited by: reubin nibuer ]
Allain Walker
Greenhorn

Joined: May 24, 2005
Posts: 14
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


5 years on and still a Greenhorn, what to do, what to do?
Geethakrishna Srihari
Ranch Hand

Joined: May 25, 2005
Posts: 30
No...the compiler problem refers to the code which i pasted.
Allain Walker
Greenhorn

Joined: May 24, 2005
Posts: 14
Sorry thought that it was the other code.
Allain Walker
Greenhorn

Joined: May 24, 2005
Posts: 14
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.
 
subject: array question
 
Similar Threads
Need explanation about three dimensional arrays
Can anyone explain this code from ...Java 2 Part-1 the programmers exam - page 49
let me know about the logic here
K&B , Chap1 Q11
Plz explain me the code