Jagged Arrays

Our dear friend Ms.Asha Bora has done some studies on the wonderful Jagged Arrays..
Lets see what she has to say..

In this Article I will be discussing about Jagged Arrays. Many of us who are working as a Programmer may not be knowing about it, as I didn't knew it. One of my friends told me about Jagged Arrays. So I thought that I should explore myself on this topic so that people like me can know more about it..

.Net supports two types of Arrays:
Single dimensional arrays(Linear)
Multi-dimensional arrays - These are of two type : Rectangular Arrays and Jagged Arrays

What are Jagged Arrays?

A jagged Array is nothing but a multidimensional array, whose elements are arrays. The elements of an array can be of different dimensions and sizes. A jagged array is also known as “array of arrays”. Following is an example to show the declaration and initialization of jagged array:

i. Declaration : int[][] jaggedArray = new int[3][];
       Over here‘3’shows the number of rows in the array.

ii. Initialization: There are many ways to initialize a jagged array. In the given example each of the element is a single dimensional array.

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


We can also do initialization of the arrays without knowing its size.
For Example :

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

We can also do this in the below given way. Notice that we cannot omit the new operator from the elements initialization because there is no default initialization for the elements..

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

NOTE: A jagged array is an array of arrays, and therefore its elements are reference types and are initialized to null. The length property of the array returns the number of arrays contained in the jagged array.

The following C# example will tell us how we can iterate in a jagged array.

class JaggedArray_Test
{
static void Main()
{
// Declare the array of two elements:
int[][] jaggedarr = new int[2][];

// Initialize the elements:
jaggedarr [0] = new int[5] { 3, 9, 15, 21, 27 };
jaggedarr [1] = new int[4] { 6, 12, 18, 24 };

// Display the array elements:
for (int i = 0; i < jaggedarr.Length; i++)
{
System.Console.Write("Element({0}): ", i);

for (int j = 0; j < jaggedarr [i].Length; j++)
{
System.Console.Write("{0}", jaggedarr [i][j]);
System.Console.Write(“ ”);
}
System.Console.WriteLine();
}
}
}
OUTPUT will be: Element(0) 3 9 15 21 27
Element(1) 6 12 18 24

Difference between multidimensional arrays (also known as rectangular arrays) and jagged arrays:

In a multi-dimensional array we have to declare both dimensions. Suppose we want to store the number of months and the days of each month in an array. If we create a multidimensional array then we have to give a fixed size to both the number of rows and column (To store the number of days for the month).But since each month has different number of days thus declaring the fixed size will lead to wastage of memory. Thus in this case we can use jagged arrays, as we don’t need to declare the second dimension of the array. It is enough to declare just the number of rows (to store number of months) in jagged array. The column or say the size of each linear array will vary as per the requirement.

THUS WE CAN SAY Jagged arrays have certain advantages over rectangular arrays. Jagged arrays are more space efficient, since not all rows (in a two-dimensional array) must have the same number of columns. In addition, since each row has its own one-dimensional array, we can easily pass an individual row to a method that requires a one-dimensional array parameter.
We will find working with jagged arrays much easier if you keep in mind that a jagged array is really an array of arrays.

References :
 http://msdn.microsoft.com/en-us/library/2s05feca(VS.80).aspx
 http://www.odetocode.com/Articles/253.aspx
 http://www.informit.com/guides/content.aspx?g=dotnet&seqNum=128


No comments: