Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Jagged Arrays in C#

Introduction

In C#, arrays are used to store multiple values in a single variable. A jagged array is an array of arrays, meaning that its elements are arrays, and these arrays can be of different sizes. Jagged arrays are also known as "array of arrays". They are useful when you need to store data in a non-uniform structure where each sub-array can have different lengths.

Declaring a Jagged Array

To declare a jagged array, you specify an array of arrays by using square brackets. Here is the syntax:

int[][] jaggedArray;

This declares a jagged array that can hold arrays of integers.

Initializing a Jagged Array

To initialize a jagged array, you need to create the individual arrays for each element of the jagged array. Here is an example:

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

This initializes a jagged array with three elements. Each of these elements is an array that can be initialized separately.

jaggedArray[0] = new int[5]; // First array with 5 elements
jaggedArray[1] = new int[3]; // Second array with 3 elements
jaggedArray[2] = new int[4]; // Third array with 4 elements

Assigning Values to a Jagged Array

After initializing the jagged array and its sub-arrays, you can assign values to the elements. Here is an example:

jaggedArray[0][0] = 1;
jaggedArray[0][1] = 2;
jaggedArray[0][2] = 3;
jaggedArray[0][3] = 4;
jaggedArray[0][4] = 5;

jaggedArray[1][0] = 6;
jaggedArray[1][1] = 7;
jaggedArray[1][2] = 8;

jaggedArray[2][0] = 9;
jaggedArray[2][1] = 10;
jaggedArray[2][2] = 11;
jaggedArray[2][3] = 12;

Accessing Elements of a Jagged Array

You can access the elements of a jagged array using the index of the sub-array and the index within that sub-array. Here is an example:

int value = jaggedArray[1][2]; // Accesses the third element of the second array

Iterating Through a Jagged Array

You can use nested loops to iterate through a jagged array. Here is an example:

for (int i = 0; i < jaggedArray.Length; i++)
{
    for (int j = 0; j < jaggedArray[i].Length; j++)
    {
        Console.Write(jaggedArray[i][j] + " ");
    }
    Console.WriteLine();
}
Output:
1 2 3 4 5
6 7 8
9 10 11 12

Example Program: Jagged Array

Here is a complete example demonstrating the declaration, initialization, and iteration through a jagged array:

using System;

class Program
{
    static void Main()
    {
        // Declare and initialize a jagged array
        int[][] jaggedArray = new int[3][];
        jaggedArray[0] = new int[5];
        jaggedArray[1] = new int[3];
        jaggedArray[2] = new int[4];

        // Assign values to the jagged array
        jaggedArray[0] = new int[] { 1, 2, 3, 4, 5 };
        jaggedArray[1] = new int[] { 6, 7, 8 };
        jaggedArray[2] = new int[] { 9, 10, 11, 12 };

        // Iterate through the jagged array
        for (int i = 0; i < jaggedArray.Length; i++)
        {
            for (int j = 0; j < jaggedArray[i].Length; j++)
            {
                Console.Write(jaggedArray[i][j] + " ");
            }
            Console.WriteLine();
        }
    }
}