Creating a Jagged Array in C#

In this example, the value of the first array element is displayed using individual element access. Additionally, the method used to determine the number of arrays contained in the jagged array is presented. An example is shown, including the data type of the array, the jagged array representation, the name of the jagged array, and the number of elements (arrays) inside the jagged array. As each element of a jagged array is an array itself, the size of individual arrays can be set.

A jagged array, also known as an “array of arrays,” consists of arrays as its elements, which may vary in size. To declare, initialize, and
access jagged arrays
, refer to the following examples.

Here is a statement about an
single-dimensional array
that consists of three components, all of which are arrays of integers with a single dimension.

int[][] jaggedArray = new int[3][];

To utilize

jaggedArray

, its components need to be initialized. The initialization of the components can be done as follows:

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

The elements are arrays of integers with one dimension. The first array contains 5 integers, the second has 4 integers, and the third has 2 integers.

You can utilize initializers to populate the array elements with values, eliminating the need for specifying the array size. An instance is:

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

One can initiate the array during declaration in the following manner:

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

The shorthand form can be utilized, but it’s important to note that the

new

operator cannot be excluded from the elements initialization. This is due to the absence of default initialization for the elements.

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

An array of arrays is called a jagged array, and as a result, its items are reference types that get initialized to

null

.

Examples like these can be used to access individual array elements.

// Assign 77 to the second element ([1]) of the first array ([0]):
jaggedArray3[0][1] = 77;
// Assign 88 to the second element ([1]) of the third array ([2]):
jaggedArray3[2][1] = 88;

You can combine jagged and multidimensional arrays by initializing an array named
single-dimensional jagged array
with three two-
dimensional array elements
of varying sizes. To learn more, refer to Multidimensional Arrays.

int[][,] jaggedArray4 = new int[3][,]
{
    new int[,] { {1,3}, {5,7} },
    new int[,] { {0,2}, {4,6}, {8,10} },
    new int[,] { {11,22}, {99,88}, {0,9} }
};

In this example, you can retrieve the value of a specific element from the first array by accessing it using its identifier, which is

[1,0]

. The value of this element is

5

.

System.Console.Write("{0}", jaggedArray4[0][1, 0]);

The function

Length

calculates the count of arrays present within the jagged array. For instance, if the array has been declared earlier, this code would provide the number of arrays:

System.Console.WriteLine(jaggedArray4.Length);

returns a value of 3.

Example

An array comprising arrays with varying sizes is generated in this instance.

class ArrayTest
{
    static void Main()
    {
        // Declare the array of two elements.
        int[][] arr = new int[2][];
        // Initialize the elements.
        arr[0] = new int[5] { 1, 3, 5, 7, 9 };
        arr[1] = new int[4] { 2, 4, 6, 8 };
        // Display the array elements.
        for (int i = 0; i < arr.Length; i++)
        {
            System.Console.Write("Element({0}): ", i);
            for (int j = 0; j < arr[i].Length; j++)
            {
                System.Console.Write("{0}{1}", arr[i][j], j == (arr[i].Length - 1) ? "" : " ");
            }
            System.Console.WriteLine();
        }
        // Keep the console window open in debug mode.
        System.Console.WriteLine("Press any key to exit.");
        System.Console.ReadKey();
    }
}
/* Output:
    Element(0): 1 3 5 7 9
    Element(1): 2 4 6 8
*/
  • Array
  • C# Programming Guide
  • Arrays
  • Single-Dimensional Arrays
  • Multidimensional Arrays

Frequently Asked Questions

Posted in Uncategorized