p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3 (http://p2p.wrox.com/forumdisplay.php?f=409)
-   -   Initializing a ragged array during run time p197 (http://p2p.wrox.com/showthread.php?t=82711)

scolty February 21st, 2011 05:23 PM

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

DrPurdum February 21st, 2011 06:13 PM

ragged arrays
 
You understand it correctly. The difficult part to understand for most people is the idea that the second element subscript is actually just an lvalue. The second set of statements:

samples[0] = new int[count1];

is simply filling in the lvalue with a pointer to an array of 3 ints. The next statement simply fills in a pointer to an array of 10 ints, and so on.

Therefore, the data look exactly as you understand it. About the only thing you can't assume is that all elements of the array are contiguous. In actual fact, Windows is free to scatter those three arrays (i.e., samples[0][0], samples[1][0], and samples[3][0]) anywhere it wants in memory space.

scolty February 21st, 2011 06:31 PM

glad to hear.

i think i have gone as far as i can at the moment and its time to run back through the book and compile all my little scribbles + revisit one or two things. As stated previously though, this is by far the best book on the subject ive read. Thanks very much for taking the time to write this, it gives me something to look forward to after a dull day at work.

Regarding the scattering of the three arrays in memory, im assuming this is similar to what is shown in figure 8.10 on page 196 except since each count is type int (4 bytes), there is no need for a third level so to speak since the int could be stored in the rvalues at 750,000 n onwards? (This is one of the topics i feel i need to go back on so i may be wrong).

Thanks again for your help.

Rgds

Scolty


All times are GMT -4. The time now is 07:39 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.