View Single Post
  #1 (permalink)  
Old February 21st, 2011, 05:23 PM
scolty scolty is offline
Authorized User
 
Join Date: Feb 2011
Posts: 21
Thanks: 2
Thanked 0 Times in 0 Posts
Default Initializing a ragged array during run time p197

Good evening,

I was wondering if someone would be so kind to explain the following code to me:

Code:
int[][] samples = new int [3][];

samples[0] = new int[count1];
samples[1] = new int[count2];
samples[2] = new int[count3];

for (i = 0; i<3; i++)
{
    samples[0][i] = i;
    samples[1][i] = i;
    samples[2][i] = i;
}
Note: the above code is on p197 (sorry if i have accidently copied it wrong)

I understand the first line where the actual array is declared, 3 rows and an undeclared number of columns.

(this is where im not sure im right)

ive understood the next section as basically selecting each row in the multi-dimensional array and declaring a 1D array (the count variable) in it. so the first row has 3 columns. the second row would have 10 columns etc.

(This is where im completely lost)

im assuming my understanding of the 2nd step is wrong as i dont understand what the last section is doing. Is it not creating an array which looks like

0,1,2,
0,1,2,0,0,0,0,0,0,0
0,1,2,0,0

??

Thanks very much for taking the time to answer my question.

Rgds

Scolty

Last edited by scolty; February 21st, 2011 at 05:25 PM..