Jagged Arrays


In the preceding examples, when you created a two-dimensional array, you were creating what C# calls a rectangular array. Thinking of two-dimensional arrays as tables, a rectangular array is a two-dimensional array in which the length of each row is the same for the entire array. However, C# also allows you to create a special type of two-dimensional array called a jagged array. A jagged array is an array of arrays in which the length of each array can differ. Thus, a jagged array can be used to create a table in which the lengths of the rows are not the same.

Jagged arrays are declared by using sets of square brackets to indicate each dimension. For example, to declare a two-dimensional jagged array, you will use this general form:

 type[ ] [ ] array-name = new type[size][ ];

Here, size indicates the number of rows in the array. The rows, themselves, have not been allocated. Instead, the rows are allocated individually. This allows for the length of each row to vary. For example, the following code allocates memory for the first dimension of jagged when it is declared. It then allocates the second dimensions manually.

 int[][] jagged = new int[3][]; jagged[0] = new int[4]; jagged[1] = new int[3]; jagged[2] = new int[5];

After this sequence executes, jagged looks like this:

image from book

It is easy to see how jagged arrays got their name!

Once a jagged array has been created, an element is accessed by specifying each index within its own set of brackets. For example, to assign the value 10 to element 2, 1 of jagged, you would use this statement:

 jagged[2][1] = 10;

Note that this differs from the syntax that is used to access an element of a rectangular array.

The following program demonstrates the creation of a jagged two-dimensional array:

 // Demonstrate jagged arrays. using System; class Jagged {   public static void Main() {     int[][] jagged = new int[3][];     jagged[0] = new int[4];     jagged[1] = new int[3];     jagged[2] = new int[5];     int i;     // store values in first array     for(i=0; i < 4; i++)       jagged[0][i] = i;     // store values in second array     for(i=0; i < 3; i++)       jagged[1][i] = i;     // store values in third array     for(i=0; i < 5; i++)       jagged[2][i] = i;     // display values in first array     for(i=0; i < 4; i++)       Console.Write(jagged[0][i] + " ");     Console.WriteLine();     // display values in second array     for(i=0; i < 3; i++)       Console.Write(jagged[1][i] + " ");     Console.WriteLine();          // display values in third array     for(i=0; i < 5; i++)       Console.Write(jagged[2][i] + " ");     Console.WriteLine();   } }

The output is shown here:

 0 1 2 3 0 1 2 0 1 2 3 4

Jagged arrays are not used by all applications, but they can be effective in some situations. For example, if you need a very large two-dimensional array that is sparsely populated (that is, one in which not all of the elements will be used), then an irregular array might be a perfect solution.

One last point: since jagged arrays are arrays of arrays, there is no restriction that requires that the arrays be single-dimensional. For example, the following creates an array of two-dimensional arrays:

 int[][,] jagged = new int[3][,];

The next statement assigns jagged[0] a reference to a 4×2 array:

 jagged[0] = new int[4, 2];

The following statement assigns a value to jagged[0][1,0]:

 jagged[0][1,0] = i;




C# 2.0(c) The Complete Reference
C# 2.0: The Complete Reference (Complete Reference Series)
ISBN: 0072262095
EAN: 2147483647
Year: 2006
Pages: 300

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net