Sep 20, 2011

Jagged array in c#

Jagged array is special type of array whose elements are also arrays. Elements can be of any dimensions and size. Elements of jagged array are reference type and its default value is null.

Examples of jagged array:
Jagged array whose all elements are one dimensional array of different size

using System;

namespace JaggedArrayDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //Declaration of jagged array
            int [][] arr = new int[3][];

            //Initialization of each elements of jagged array
            arr[0] = new int[3]{2,4,6};
            arr[1]= new int[5]{1,3,5,7,9};
            arr[2]= new int[1]{0};

            //printing the size of each element of jagged array
            Console.WriteLine("Size of element 1: {0}", arr[0].Length);
            Console.WriteLine("Size of element 2: {0}", arr[1].Length);
            Console.WriteLine("Size of element 3: {0}", arr[2].Length); 

            //printing the each elements of jagged array
            for (int i = 0; i < arr.Length; i++)
            {
                for (int j = 0; j < arr[i].Length; j++)
                {
                    Console.Write("{0} ", arr[i][j]);

                }
                Console.WriteLine();
            }
            Console.ReadKey();
        }
    }
}

Output:
Size of element 1: 3
Size of element 2: 5
Size of element 3: 1
2 4 6
1 3 5 7 9
0

Initializing the jagged array at the time of declaration:

using System;

namespace JaggedArrayDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //Declaration and initializations of jagged array
            int[][] arr = new int[][]{
                new int[]{2,4,6},
                new int[]{1,3}
            };

            //Printing each elements of jagged array
            for (int i = 0; i < arr.Length; i++)
            {
                for (int j = 0; j < arr[i].Length; j++)
                {
                    Console.Write("{0} ", arr[i][j]);
                }
                Console.WriteLine();
            }
            Console.ReadKey();
           
        }
    }
}

Output:
2 4 6
1 3

Difference between arr[][] and arr[,] in C#

(I) arr[,] is two dimensional array while arr[][] is jagged array.
(II)In two dimensional arrays it is necessary to specify the size of each dimensions of array at the time of declaration. For example:
int[,] arr = new int[2,5];
or
int [,] arr = new int[,]{{1,2},{4,6,8}};

But it is error to declare like
int[,] arr1 = new int[2,];

While in case of jagged array declaration we can specify the size of only first dimension. For example:
int[][] arr = new int[2][];

It is error to declare like
int[][] arr = new int[2][5];

(III) Each element of jagged array must be an array. For example:
int[][] arr = new int[2][];

arr[0] = new int[5];
It is error to write:

arr[0] = 5;

While in case of multi dimensional array no any  elements can be array. For example:
 int[,] arr = new int[2,5];
 int[0][0] = 5;
           
It is error to write:
arr[0] = new int[5];

One dimensional jagged array whose each elements are two dimensional arrays

using System;

namespace JaggedArrayDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //Declaration and inititalization of jagged array
            int[][,] arr = new int[2][,];
           
            arr[0]= new int[,]{{1,2},{3,4}};
            arr[1] = new int[,]{{1,2},{4,8},{8,16}};
 
            foreach(int[,] temparr in arr )
            {
                for (int i = 0; i < temparr.Length / temparr.Rank; i++)
                {
                    for (int j = 0; j < temparr.Rank; j++)
                    {
                        Console.Write("{0} ",temparr[i, j]);
                    }
                    Console.WriteLine();
                }
                Console.WriteLine();
            }
            Console.ReadKey();           
        }
    }
}

Output:
1 2
3 4

1 2
4 8
8 16


Two dimensional jagged array whose each elements are single dimensional array

No comments:

Post a Comment