C# Jagged Arrays with Examples

  Keywords : C# jagged arrays to represent the data in different dimensions and sizes with examples, c# jagged arrays called as array of arrays and its elements are reference types

Here we will learn jagged arrays in c# with examples and how to use c# jagged arrays to represent an elements as arrays with different dimensions and sizes with examples.

C# Jagged Arrays

In c#, Jagged Array is an array whose elements are arrays with different dimensions and sizes. Sometimes jagged array called as “array of arrays” and it can store arrays instead of particular data type value.

C# Jagged Array Declaration

In c#, a jagged array can be initialized with two square brackets [][]. The first square bracket will specify the size of an array and the second one will specify the dimension of array which is going to be stored as a value.

 

Following are the examples of creating jagged arrays in c# programming language with single and multidimensional arrays.

 

// Jagged Array with Single Dimensional Array

int[][] jarray = new int[2][];

// Jagged Array with Two Dimensional Array

int[][,] jarray1 = new int[3][,];

If you observe above examples, first array (jarray) is allowed to store 2 elements of single dimensional arrays and second array (jarray1) is allowed to store 3 elements of multidimensional arrays.

C# Jagged Array Initialization

In c#, we can initialize an arrays upon declaration. Following are the different ways of declaring and initializing a jagged arrays in c# programming language.

 

// Jagged Array with Single Dimensional Array

int[][] jarray = new int[3][];

jarray[0] = new int[5] { 1, 2, 3, 4, 5 };

jarray[1] = new int[3] { 10, 20, 30 };

jarray[2] = new int[] { 12, 50, 60, 70, 32 };

// Jagged Array with Two Dimensional Array

int[][,] jarray1 = new int[3][,];

jarray1[0] = new int[2, 2] { { 15, 24 }, { 43, 54 } };

jarray1[1] = new int[,] { { 11, 12 }, { 13, 14 }, { 25, 26 } };

jarray1[2] = new int[4, 3];

// Initializing an Array on Declaration

int[][] jarray2 = new int[][]

{

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

new int[]{98, 56, 45},

new int[]{32}

};

If you observe above examples, we declared and initialized a jagged arrays with single dimensional and multidimensional arrays as an elements in different ways.

 

In jagged arrays size of elements is optional so in some of elements we didn’t mentioned a size to store single or multidimensional arrays with different sizes based on our requirements.

C# Access Jagged Array Elements

In c#, we can access the values of a jagged arrays by using row index and column index values.

 

Following is the example of accessing an elements from jagged arrays in c# programming language based on our requirements.

 

int i = jarray[0][2]; //3

int j = jarray[2][1]; //50

int k = jarray1[0][1, 1]; //54

int l = jarray1[1][2, 1]; //26

If you observe above example, we are accessing an elements of single or multidimensional arrays by passing index values.

C# Jagged Array Example

Following is the example of using a jagged arrays in c# programming language to represent arrays as an elements in array with single or multiple dimensions.

 

using System;

 

namespace Tutlane

{

    class Program

    {

        static void Main(string[] args)

        {

            // Jagged Array with Single Dimensional Array

            int[][] jarray = new int[3][];

            jarray[0] = new int[5] { 1, 2, 3, 4, 5 };

            jarray[1] = new int[3] { 10, 20, 30 };

            jarray[2] = new int[] { 12, 50, 60, 70, 32 };

            Console.WriteLine("---Jagged Array with Single Dimensional Elements---\n");

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

            {

                Console.Write("Element[{0}]: ", i);

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

                {

                    Console.Write("{0}{1}", jarray[i][j], j == (jarray[i].Length - 1) ? "" : " ");

                }

                Console.WriteLine();

            }

            // Jagged Array with Two Dimensional Array

            int[][,] jarray1 = new int[2][,];

            jarray1[0] = new int[2, 2] { { 15, 24 }, { 43, 54 } };

            jarray1[1] = new int[,] { { 11, 12 }, { 13, 14 }, { 25, 26 } };

            Console.WriteLine("\n---Jagged Array with Mult-Dimensional Elements---\n");

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

            {

                Console.Write("Element[{0}]: ", i);

                for (int j = 0; j < jarray1[i].GetLength(0); j++)

                {

                    Console.Write("{");

                    for (int k = 0; k < jarray1[i].GetLength(1); k++)

                    {

                        Console.Write("{0}{1}", jarray1[i][j, k], k == (jarray1[i].GetLength(1) - 1) ? "" : " ");

                    }

                    Console.Write("{0}{1}", "}", j < jarray1.GetLength(0) ? ", " : "");

                }

                Console.WriteLine();

            }

            Console.WriteLine("\nPress Any Key to Exit..");

            Console.ReadLine();

        }

    }

}

If you observe above example, we are creating jagged array with single & multidimensional arrays and we used for loop in c# to get the elements of jagged array.

 

When we run above c# program, we will get the result like as shown below.

 

C# Jagged Array Example Result

 

If you observe above result, we stored arrays as an elements in jagged array based on our requirements.

 

This is how we can use jagged arrays in our c# applications to store single or multi-dimensional arrays as an element of jagged array.