Geekpedia Tutorials Home

Building a C# Chat Client and Server

Building a C# Chat Client and ServerA step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.

in C# Programming Tutorials

Getting Hard Drive Information

Getting Hard Drive InformationA C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.

in C# Programming Tutorials

UPS Shipping Calculator

UPS Shipping CalculatorThis tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.

in PHP Programming Tutorials

Create Your Own Rich Text Editor

Create Your Own Rich Text EditorCreating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.

in JavaScript Programming Tutorials
Search
Tutorials
Programming Tutorials
IT Jobs
From CareerBuilder

Jagged arrays

This tutorial explains what a jagged array by giving an example that shows you how set and retrieve values.

On Sunday, July 25th 2004 at 11:34 PM
By Andrew Pociu (View Profile)
*****   (Rated 4.6 with 16 votes)
Contextual Ads
More C# Resources
Advertisement
They say a jagged array is an array of arrays.

It's not so hard to comprehend this. In the following example we declare jaggedArray named jArray that can hold 3 other arrays:




int[][] jArray = new int[3][];



Next you can set the size (number of elements) of each of the 3 arrays:





jArray[0] = new int[2];

jArray[1] = new int[5];

jArray[2] = new int[7];



The first array (0) has 2 elements, the second (1) has 5 elements and the third (2) has 7 elements.



How do we assign and retrieve values from the elements of each array?

As you can see in the followin graphic, first we specify the number of the array, second we specify the number of the element:








In the following example we assign values to all the elements in all 3 arrays:





// Fill the array no. 0

jArray[0][0] = 10;

jArray[0][1] = 11;



// Fill the array no. 1


jArray[1][0] = 20;

jArray[1][1] = 21;

jArray[1][2] = 22;

jArray[1][3] = 23;

jArray[1][4] = 24;



// Fill the array no. 2


jArray[2][0] = 30;

jArray[2][1] = 31;

jArray[2][2] = 32;

jArray[2][3] = 33;

jArray[2][4] = 34;

jArray[2][5] = 35;

jArray[2][6] = 36;



Using two loops we can retrieve the values of the elements from all the arrays inside the jagged array:





// Loop through the 3 arrays

for
(int i = 0; i < jArray.GetLength(0); i++)

{

   // Loop through the elements of the current array


   for(int x = 0; x < jArray[i].GetLength(0); x++)

   {

      Console.WriteLine(jArray[i][x].ToString());

   }

   Console.WriteLine("------");

}



If you compile the code, here's the result in the console window:





10

11

------

20

21

22

23

24

------

30

31

32

33

34

35

36

------

Press any key to continue
Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this tutorial
Comment Current Comments
by disantha on Thursday, September 2nd 2004 at 04:18 AM

what's the GetLength do? i can't understand it properly

by LeVirus Watts on Saturday, August 5th 2006 at 01:23 PM

disantha,
The <array>.GetLength(x) ask the program how long the array in question is. (note: the x is which dimintion we are asking for.)

by onome ofu on Friday, March 2nd 2007 at 03:28 AM

This is a beautiful job.

by on Sunday, November 11th 2007 at 05:10 AM

thanks

by Haider on Monday, December 28th 2009 at 09:53 AM

very very easy tutorial im highly impressed from the way Andrew brother illustrated and demonstrated jagged array and now i completely understand, i read 3 ebooks but didnt understand how to use jagged arrays untill now because of their technical jorgans and shitty examples with mathematical complexities :)

by Larson on Friday, January 28th 2011 at 04:42 PM

Thanks man - I concur with Haider :-). Still being used through 2011.

by dwi on Monday, April 18th 2011 at 08:51 AM

thx man, this is what i want to search

by balance transfer credit card on Monday, December 12th 2011 at 05:34 AM

Use System.Windows.Forms.Cursor.Position to get the cursor position in screen coordinates.I admire the important information you offer within your content. I'll bookmark your web site and have my kids examine up the following typically.

by Margarita on Saturday, January 14th 2012 at 01:36 AM

Once you have recreated the problem and captured these steps, you can save them to a file and send it to your support person, who can then open it up and view

by beats by dre rouge on Thursday, March 1st 2012 at 08:50 PM

This article is very interesting, sharing is a wise choice, would like to see more about yourarticle

by michael kors bags on Sunday, March 18th 2012 at 08:55 PM

Thank you for sharing, I will often come to patronize your site

by camping lanterns on Thursday, April 12th 2012 at 05:50 AM

Much appreciated for the information and share!


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Tutorials
There are no related tutorials.

Comment Related Source Code
There is no related source code.

Jobs C# Job Search
My skills include:
Enter a City:

Select a State:


Advanced Search >>
Sponsors
Discover Geekpedia
Other Resources
Bargain EZ Tech Coupons & Deals