aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Arrays... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Arrays..." Watch "Arrays..." New topic
Author

Arrays...

sonir shah
Ranch Hand

Joined: Nov 01, 2001
Posts: 435
If size = 4, triArray looks like:

a)
1 2 3 4
5 6 7
8 9
10
b)
1 4 9 16
c)
1 2 3 4
d)
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
e)
1
2 3
4 5 6
7 8 9 10

I cannot understand what the question is trying to say?
Can any one will help me out here?..
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

Sonir,
I hope you got enough rest for your exam today.
The answer is (e).
The makeArray method creates the following array:
{
{1},
{2, 3},
{4, 5, 6},
{7, 8, 9, 10}
}
Notice the outer array has 4 elements. This results from the statement
int[][] triArray = new int[size][];
Which, when size=4, translates to "create an array that can hold 4 arrays of int".
The for (int i ... ) loop just iterates through these four elements. During each iteration, the following happens:
A new array of int is created and assigned to the
ith element of the outer array in the statement
triArray[i] = new int[i+1]
E.g., when i = 0, the above would evaluate to
triArray[0] = new int[1]; // nested array holds 1 int
and when i = 3, it would evaluate to'
triArray[3] = new int[4]; // nested array holds 4 ints
The for (int j ... ) loop simply assigns values (using and incrementing val) to the elements of the new nested array.
HTH


Junilu - [How to Ask Questions] [How to Answer Questions]
Younes Essouabni
Ranch Hand

Joined: Jan 13, 2002
Posts: 479
I do totally agree with Junilu.
int[][] triArray = new int[size] [];//At this point you create an array of 2 dimension
for( int i = 0; i < triArray.length; i++ )//In an array of 2 dimension, Array.length will always refer to the first [] of the array, hence size.
triArray[i] = new int[i+1];// Not much to explain
for( int j=0; j < triArray[i].length; j++ )// An array of 2 dimension is in fact an array inside an array, so triArray[i].length refer to the second dimension of the array. The first time the value of triArray[i].length will be 1, second time 2,... Because each time you create an array of that dimension inside the outer array. You do it at this line: triArray[i][j] = val++;
If you want to see the execution of the code you can use the code provided here:

I hope it helped you!!!
See you soon
Younes


Younes
By constantly trying one ends up succeeding. Thus: the more one fails the more one has a chance to succeed.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Arrays...