views:

171

answers:

3

What is a jagged array in c#? Any examples and when should one use it....

+8  A: 

A jagged array is an array of arrays.

string[][] arrays = new string[5][];

That's a collection of 5 different string arrays, each could be a different length.

arrays[0] = new string[5];
arrays[1] = new string[100];
...

This is different from a 2D array where it is rectangular, meaning each row has the same number of columns.

string[,] array = new string[3,5];
Anthony Pegram
+1  A: 

A jagged array is the same in any language, but it's where you have a 2+ dimensional array with different array lengths in the second and beyond array.

[0] - 0, 1, 2, 3, 4
[1] - 1, 2, 3
[2] - 5, 6, 7, 8, 9, 10
[3] - 1
[4] - 
[5] - 23, 4, 7, 8, 9, 12, 15, 14, 17, 18
Slokun
+1  A: 

You can find more information here : http://msdn.microsoft.com/en-us/library/2s05feca.aspx

Also :

A jagged array is an array whose elements are arrays. The elements of a jagged array can be of different dimensions and sizes. A jagged array is sometimes called an "array of arrays." The following examples show how to declare, initialize, and access jagged arrays.

The following is a declaration of a single-dimensional array that has three elements, each of which is a single-dimensional array of integers:

jaggedArray[0] = new int[5];
jaggedArray[1] = new int[4];
jaggedArray[2] = new int[2];

or

jaggedArray[0] = new int[] { 1, 3, 5, 7, 9 };
jaggedArray[1] = new int[] { 0, 2, 4, 6 };
jaggedArray[2] = new int[] { 11, 22 };
Braveyard