Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3
This is the forum to discuss the Wrox book Beginning C# 3.0 : An Introduction to Object Oriented Programming by Jack Purdum; ISBN: 9780470261293
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 21st, 2011, 05:23 PM
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..
  #2 (permalink)  
Old February 21st, 2011, 06:13 PM
Friend of Wrox
Points: 1,166, Level: 13
Points: 1,166, Level: 13 Points: 1,166, Level: 13 Points: 1,166, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2008
Location: Indianapolis, IN, USA.
Posts: 234
Thanks: 0
Thanked 32 Times in 30 Posts
Default 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.
__________________
Jack Purdum, Ph.D.
Author: Beginning C# 3.0: Introduction to Object Oriented Programming (and 14 other programming texts)
  #3 (permalink)  
Old February 21st, 2011, 06:31 PM
Authorized User
 
Join Date: Feb 2011
Posts: 21
Thanks: 2
Thanked 0 Times in 0 Posts
Default

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
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Store time into Array derekl ASP.NET 1.0 and 1.1 Basics 3 December 18th, 2006 09:41 AM
Design-Time or Run-Time now ? ALGNET .NET Framework 2.0 1 July 31st, 2006 04:43 AM
initializing variables vin_0x1 Classic ASP Components 2 April 16th, 2006 07:59 PM
Two dimensional array with run time error ie_lml Excel VBA 1 November 23rd, 2004 12:54 PM
Initializing a database davest Access 4 January 16th, 2004 10:08 AM



All times are GMT -4. The time now is 06:20 AM.


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