0
0
C Sharp (C#)programming~5 mins

Jagged arrays in C Sharp (C#)

Choose your learning style9 modes available
Introduction

Jagged arrays let you store arrays inside an array, where each inner array can have a different size. This helps when you want to group related data but the groups are not all the same length.

When you want to store a list of student grades, but each student has taken a different number of tests.
When you need to represent a calendar where each month has a different number of days.
When you want to store rows of data where each row can have a different number of columns.
When you want to save memory by not forcing all inner arrays to be the same size.
Syntax
C Sharp (C#)
class Program
{
    static void Main()
    {
        // Declare a jagged array with 3 rows
        int[][] jaggedArray = new int[3][];

        // Initialize each row with different lengths
        jaggedArray[0] = new int[2];
        jaggedArray[1] = new int[4];
        jaggedArray[2] = new int[3];

        // Assign values
        jaggedArray[0][0] = 1;
        jaggedArray[0][1] = 2;

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

        jaggedArray[2][0] = 7;
        jaggedArray[2][1] = 8;
        jaggedArray[2][2] = 9;
    }
}

Each element of the main array is itself an array, which can have different lengths.

You must initialize each inner array separately before using it.

Examples
This creates a jagged array with zero rows. It has no inner arrays yet.
C Sharp (C#)
int[][] jaggedArray = new int[0][]; // Empty jagged array with no rows
Here, the jagged array has one row, but that row is an empty array.
C Sharp (C#)
int[][] jaggedArray = new int[1][];
jaggedArray[0] = new int[0]; // One row with zero columns
The first row has one element, the second row has three elements.
C Sharp (C#)
int[][] jaggedArray = new int[2][];
jaggedArray[0] = new int[1] { 10 };
jaggedArray[1] = new int[3] { 20, 30, 40 };
Each row is initialized with different numbers of elements directly.
C Sharp (C#)
int[][] jaggedArray = new int[3][];
jaggedArray[0] = new int[] { 1, 2 };
jaggedArray[1] = new int[] { 3, 4, 5 };
jaggedArray[2] = new int[] { 6 };
Sample Program

This program creates a jagged array with 3 rows, each with different lengths. It prints the array, changes one element, then prints it again to show the change.

C Sharp (C#)
using System;

class Program
{
    static void Main()
    {
        // Create a jagged array with 3 rows
        int[][] jaggedArray = new int[3][];

        // Initialize each row with different lengths
        jaggedArray[0] = new int[] { 1, 2 };
        jaggedArray[1] = new int[] { 3, 4, 5, 6 };
        jaggedArray[2] = new int[] { 7, 8, 9 };

        Console.WriteLine("Jagged array before changes:");
        PrintJaggedArray(jaggedArray);

        // Change one value
        jaggedArray[1][2] = 50;

        Console.WriteLine("\nJagged array after changing jaggedArray[1][2] to 50:");
        PrintJaggedArray(jaggedArray);
    }

    static void PrintJaggedArray(int[][] array)
    {
        for (int row = 0; row < array.Length; row++)
        {
            Console.Write("Row " + row + ": ");
            for (int col = 0; col < array[row].Length; col++)
            {
                Console.Write(array[row][col] + " ");
            }
            Console.WriteLine();
        }
    }
}
OutputSuccess
Important Notes

Accessing an uninitialized inner array will cause a NullReferenceException.

Time complexity to access an element is O(1) because you use two indexes.

Jagged arrays use less memory than rectangular arrays when inner arrays have different lengths.

Use jagged arrays when rows have varying sizes; use rectangular arrays when all rows have the same size.

Summary

Jagged arrays are arrays of arrays where inner arrays can have different lengths.

You must initialize each inner array separately before using it.

They are useful to store grouped data with varying sizes efficiently.