Jagged Arrays in C#.NET


In this article I will explain about jagged array in C#.NET

 

Jagged Arrays (array-of-arrays)

 

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."

 

A special type of array is introduced in C#. A Jagged Array is an array of an array in which the length of each array index can differ.

 

Example:

 

int[][] jagArray = new int[5][];

 

In the above declaration the rows are fixed in size. But columns are not specified as they can vary.

 

Declaring and initializing jagged array.

 

int[][] jaggedArray = new int[5][];

 

jaggedArray[0] = new int[3];

jaggedArray[1] = new int[5];

jaggedArray[2] = new int[2];

jaggedArray[3] = new int[8];

jaggedArray[4] = new int[10];

 

jaggedArray[0] = new int[] { 3, 5, 7, };

jaggedArray[1] = new int[] { 1, 0, 2, 4, 6 };

jaggedArray[2] = new int[] { 1, 6 };

jaggedArray[3] = new int[] { 1, 0, 2, 4, 6, 45, 67, 78 };

jaggedArray[4] = new int[] { 1, 0, 2, 4, 6, 34, 54, 67, 87, 78 };

 

You can also initialize the array upon declaration like this:

 

int[][] jaggedArray = new int[][]

{

new int[] { 3, 5, 7, },

new int[] { 1, 0, 2, 4, 6 },

new int[] { 1, 6 },

new int[] { 1, 0, 2, 4, 6, 45, 67, 78 }

};

 

You can use the following shorthand form:

 

int[][] jaggedArray =

{

new int[] { 3, 5, 7, },

new int[] { 1, 0, 2, 4, 6 },

new int[] {1, 2, 3, 4, 5, 6, 7, 8},

new int[] {4, 5, 6, 7, 8}

};

 

Note: Notice that you cannot omit the new operator from the elements initialization because there is no default initialization for the elements.


jagged_array.gif

Jagged array real life example

 

You can consider jagged array as a movie ticket counter where ticket selling counters are fixed (rows are fixed) but you don't know how many people will be standing in each counter in a queue (column numbers are not fixed can vary on different rows).

 

A jagged array is an array of arrays, and therefore its elements are reference types and are initialized to null.

 

It is possible to mix jagged and multidimensional arrays. The following is a declaration and initialization of a single-dimensional jagged array that contains two-dimensional array elements of different sizes:

 

int[][,] jaggedArray = new int[4][,]

{

new int[,] { {11,23}, {58,78} },

new int[,] { {50,62}, {45,65}, {85,15} },

new int[,] { {245,365}, {385,135} },

new int[,] { {1,2}, {4,4}, {4,5} }

};

 

We should be careful using methods like GetLowerBound(), GetUpperBound, GetLength, etc. with jagged arrays. Since jagged arrays are constructed out of single-dimensional arrays, they shouldn't be treated as having multiple dimensions in the same way that rectangular arrays do.

 

Practical demonstration of jagged array of integers

 

using System;

 

namespace jagged_array

{

    class Program

    {

        static void Main(string[] args)

        {

            // Declare the array of four elements:

            int[][] jaggedArray = new int[4][];

 

            // Initialize the elements:

            jaggedArray[0] = new int[2] { 7, 9 };

            jaggedArray[1] = new int[4] { 12, 42, 26, 38 };

            jaggedArray[2] = new int[6] { 3, 5, 7, 9, 11, 13 };

            jaggedArray[3] = new int[3] { 4, 6, 8 };

 

            // Display the array elements:

            for (int i = 0; i < jaggedArray.Length; i++)

            {

                System.Console.Write("Element({0}): ", i + 1);

 

                for (int j = 0; j < jaggedArray[i].Length; j++)

                {

                    System.Console.Write(jaggedArray[i][j] + "\t");

                }

                System.Console.WriteLine();

            }

            Console.ReadLine();

        }

    }

}
 


Run the code to see the output.
 

jagged_array_output.gif
 

Practical demonstration of jagged array of string

 

using System;

 

namespace jagged_array

{

    class Program

    {

        static void Main(string[] args)

        {

            string[][] Members = new string[4][]{

               new string[]{"Rocky", "Sam", "Alex"},

               new string[]{"Peter", "Sonia", "Prety", "Ronnie", "Dino"},

            new string[]{"Yomi", "John", "Sandra", "Alex", "Tim", "Shaun"},

            new string[]{"Teena", "Mathew", "Arnold", "Stallone", "Goddy", "Samson", "Linda"},

                };

 

            for (int i = 0; i < Members.Length; i++)

            {

                System.Console.Write("Name List ({0}): ", i + 1);

 

                for (int j = 0; j < Members[i].Length; j++)

                {

                    System.Console.Write(Members[i][j] + "\t");

                }

                System.Console.WriteLine();

            }

            Console.ReadKey();

        }

    }

}

 

Run the code to see the output.


jagged_array_output2.gif
 

Conclusion

 

Well, I hope that you've benefited from this article. Your feedback and constructive contributions are welcome.  Please feel free to contact me for feedback or comments you may have about this article.