Topic :Jagged Array | Arrays of Arrays | In C# Programming Language:



Jagged Array:

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

Jagged Arrays are a Special Type of Arrays that can be used to store rows of data having different length to improve performance when working with Multi-Dimensional Arrays:



Declaring Jagged Arrays:

When Declaring a Jagged Array, We can just declare the Number of rows of the Array and prefer to specify the Number of Columns at Run-time.



In Jagged Array, The Number of Rows is Fixed, But the Number of Columns could be changed.


 
//Declaring Jagged Arrays:

Data-Type [][]  Variable-Name = new Data-Type [5][];


  • In Above Syntax:

    » Data-Type could be any valid data type like int, or float, or char etc.
    » [][] represent Array. the first Bracket Represent Row, and Second Represent column.
    » Variable-Name could be Any variable Name. The rules to chose variable-name is same as for variables or identifiers.
    » [5][] Represent that, Rows are fixed in size (i,e 5), But columns are not specified as They can be change.



Initializing Jagged Array.

We can Initialize the Jagged Array upon Declaration like this:


 int[][] JagArray = new int[][]
 {
    new int[] {1, 2, 3},
    new int[] {4, 5, 6, 7},
    new int[] {8, 9, 10, 11, 12, 13},
    new int[] {14, 15, 16}
 };


Or We can Initialize the Jagged Array using this shorthand form too.


 int[][] JagArray = 
 {
    new int[] {1, 2, 3},
    new int[] {4, 5, 6, 7},
    new int[] {8, 9, 10, 11, 12, 13},
    new int[] {14, 15, 16}
 };


Jagged Array Example:

let's have an Example to know how exactly we can declare and initialize the Jagged array. and how this jagged array work in C#.


/*Example: Jagged Array : InfoBrother:*/

using System;
namespace ArrayApplication
{
    class JaggedArray
    {
        static void Main(string[] args)
        {
            /* a jagged array of 5 array of integers*/
            int[][] jagAray = new int[][]
            {
                new int[] { 0, 1, 2},
                new int[] { 3, 4, 5},
                new int[] { 6, 7, 8},
                new int[] { 9, 10, 11},
                new int[] { 12, 13, 14}
            };

            /* output each array element's value */
            for (int x=0; x < 5; x++)    //for rows (x):
            {
                for (int y=0; y < 3; y++)  //for columns (y):
                {
                    Console.WriteLine("jagAray[{0}][{1}] = {2}", x, y, jagAray[x][y]);
                }
            }
            Console.ReadKey();
        }
    }
}


Let's have another Example of Jagged Arrays. in This example, we will declared five rows in the Array that will contain five string arrays of Different Lengths. we will try to show some Programming Language in This example using Jagged Array:



/*Example : Jagged Array: InfoBrother:*/

using System;

namespace jagged_array
{
    class Program
    {
        static void Main(string[] args)
        {
            string[][] Language = new string[4][]
            {
               new string[]{"C++", "Csharp", "Java"},
               new string[]{"JavaScript", "PHP", "HTML", "CSS", "JQuery"},
               new string[]{"Python", "Ruby", "SQL", "SVG", "Pascal", "VBScript"},
               new string[]{"Perl", "Lua", "Pascal", "ASP.NET", "C", "AppMl", "Bootstrap"},
            };


            for (int x = 0; x < Language.Length; x++) //for rows:
            {
                System.Console.Write("Language list:  ({0}): ", x + 1);

                for (int y = 0; y < Language[x].Length; y++)  //for Columns:
                {
                    System.Console.Write(Language[x][y] + "\t");
                }
                System.Console.WriteLine();
            }
            Console.ReadKey();
        }
    }
}


Jagged Array With Multi-Dimensional Arrays:

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[5][,]
 {
    new int[,] { {1, 2}, {3, 4} },
    new int[,] { {5, 6}, {7, 8}, {9, 10} },
    new int[,] { {11, 12}, {12, 14} },
    new int[,] { {15, 16}, {17, 18}, {19, 20} },
    new int[,] { {21, 22}, {23, 24}, {25, 26} }
 };
















I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement