Programming References and tutorials for Developers.

c# Jagged Arrays

By The Saint on Thursday, January 29, 2009

Filed Under:

c# Jagged Arrays
When you create a multidimensional array, you always create a structure that has the same number of elements in each of the rows. For example, look at the following array declaration:

int[,] month = new int[12,30]; //Day of each month

This array assumes each row (month) has the same number of elements (days), when we know that some months have 30 days, some have 31, and one month has 29. With the array we’ve just declared, there will be several empty elements in the array. This isn’t much of a problem for this array, but with a much larger array we end up with a lot of wasted space.

The solution to this problem is to use a jagged array instead of a twodimensional array. A jagged array is an array of arrays where each row of an array is made up of an array. Each dimension of a jagged array is a onedimensional array.We call it a “jagged” array because the number of elements in each row may be different. A picture of a jagged array would not be square or rectangular, but would have uneven or jagged edges.

A jagged array is declared by putting two sets of parentheses after the array variable name. The first set of parentheses indicates the number of rows in the array. The second set of parentheses is left blank. This marks the place for the one-dimensional array that is stored in each row. Normally, the number of rows is set in an initialization list in the declaration statement, like this:

int[][] month = new int[12][];

This statement looks strange, but makes sense when you break it down. jagged is an Integer array of 12 elements, where each of the elements is also an Integer array. The initialization list is actually just the initialization for the rows of the array, indicating that each row element is an array of 12 elements, with each element initialized to the default value.

To initialize the size n element of the inner array:

month[0] = new int[31]; // for the month of Jan
month[1] = new int[29]; // for the month of Feb
month[2] = new int[31]; // for the month of Mar
. . .
month[11] = new int[31]; // for the month of Dec



Once the jagged array is declared, the elements of the individual row arrays can be assigned values. The following code fragment assigns values to jaggedArray:

month[0][0] = 23;
month[0][1] = 13;
. . .
month[7][5] = 45;

The first set of parentheses indicates the row number and the second set indicates the element of the row array. The first statement accesses the first element of the first array, the second element access the second element of the first array, and the third statement accesses the sixth element of the eighth array.

Example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ArraysBlogs
{
class Program
{
static void Main(string[] args)
{
int[][] month = new int[12][];

month[0] = new int[31];
month[1] = new int[29];
month[2] = new int[31];

//Inserting a values in jagged Array

month[0][0] = 25;
month[0][1] = 50;
month[0][2] = 75;
month[1][0] = 85;
month[1][1] = 95;
month[1][2] = 105;

//Accessing the values in jagged Array
Console.WriteLine(month[0][0]);
Console.WriteLine(month[0][1]);
Console.WriteLine(month[0][2]);
Console.WriteLine(month[1][0]);
Console.WriteLine(month[1][1]);
Console.WriteLine(month[1][2]);

Console.ReadKey();
}
}
}



0 comments for this post