Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 29th, 2004, 09:55 AM
Authorized User
 
Join Date: Jun 2004
Posts: 99
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to hlchuah77
Default jagged array problems, who can help me?

Hi everybody,

  I have problems here in using jagged array. Consider the following statements:

//line 1 int[][] jaggedArray;
//line 2 jaggedArray = new int[3][];
//line 3 jaggedArray[0] = new int[2] {1, 2};
//line 4 jaggedArray[1] = new int[3] {3, 4, 5};
//line 5 jaggedArray[2] = new int[4] {6, 7, 8, 9};


//line 6 foreach(int[] array in jaggedArray)
//line 7 {
//line 8 foreach(int subArray in array)
//line 9 {
//line 10
                         Console.WriteLine(subArray);
//line 11 }
//line 12 }

  Questions:

  1. At line 2, what does "3" means? Is it means that the jagged array is containing 3 rows of sub-arrays?

  2. From Wrox C# book, I came to know that to loop through the jagged array, we have to use nested "foreach" loops as above.The output of the statements above will generate integer numbers from 1 to 9 printed on the output screen.

How if I want to print only the output from either "jaggedArray[0]" or "jaggedArray[1]" or "jaggedArray[2]", or any possible combinations not more than two sub arrays, so is there any way to do it? For example, i might want to generate integers from 1-2 on the screen, not 1-9.

  3. Beside using "foreach" loops to loop through jaggedArray, is "for" loops capable of generating the same result? If yes, how? If no, then is there any alternative way for doing that?

I really appreciate that if anyone can explain to me clearly on this. Thank you.

 
Old June 29th, 2004, 10:47 AM
tnd tnd is offline
Authorized User
 
Join Date: Jun 2004
Posts: 40
Thanks: 0
Thanked 0 Times in 0 Posts
Default

1. Yes

2. You are certainlly welcome to use
        foreach(int subArray in jaggedArray[0])
        {
        Console.WriteLine(subArray);
        }
    to print 1 and 2.

3. You can use FOR loops if you know or can determine the upper and lower bounds of the array at run time. FOREACH however is the ideal way to do it.

 
Old July 1st, 2004, 10:37 AM
Authorized User
 
Join Date: Jun 2004
Posts: 99
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to hlchuah77
Default

Thank you very much. You really solve my problems. Seeming that I am a very beginner in C#, I really appreciate a lot. Hoping that everybody will as kind as you when I have problems at the later date. :)






Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing jagged array to com object nutrino VB How-To 0 January 27th, 2006 01:07 PM
passing jagged array of objects nutrino Beginning VB 6 0 January 27th, 2006 11:58 AM
jagged array help oozener C++ Programming 1 December 4th, 2005 10:45 PM
Array Problems Angel1701 J2EE 0 July 13th, 2004 07:50 AM
Array Structure problems Ciarano VB How-To 6 March 4th, 2004 07:10 AM





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