C# Jagged Arrays

Introduction

Jagged arrays, also known as "array-of-arrays", are arrays whose elements are also arrays. The elements of a jagged array can be of different dimensions and sizes, which provides flexibility in managing data structures that are not uniform. Jagged arrays are particularly useful when working with data that naturally fits into a hierarchical or uneven structure, such as a list of lists.

Syntax

dataType[][] arrayName = new dataType[size][];

Declaration and Initialization

Declaration

You first declare the jagged array without specifying the size of the sub-arrays.

int[][] jaggedArray;

Initialization

You initialize the jagged array by specifying the size of the main array and the sizes of the sub-arrays.

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

Combining Declaration and Initialization

You can also declare and initialize a jagged array in one line.

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

Accessing Elements

You access elements in a jagged array using two indices: the first for the main array and the second for the sub-array.

Example

using System;

namespace JaggedArrayExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Declare and initialize a jagged array
            int[][] jaggedArray = new int[3][]
            {
                new int[2] { 1, 2 },
                new int[3] { 3, 4, 5 },
                new int[4] { 6, 7, 8, 9 }
            };

            // Access and display array elements
            for (int i = 0; i < jaggedArray.Length; i++)
            {
                Console.Write($"Row {i}: ");
                for (int j = 0; j < jaggedArray[i].Length; j++)
                {
                    Console.Write(jaggedArray[i][j] + " ");
                }
                Console.WriteLine();
            }

            // Modify an array element
            jaggedArray[1][2] = 10;
            Console.WriteLine($"Modified element at [1][2]: {jaggedArray[1][2]}");
        }
    }
}

Output

Row 0: 1 2
Row 1: 3 4 5
Row 2: 6 7 8 9
Modified element at [1][2]: 10

Practical Example

Let’s create a practical example where we use a jagged array to store and display student grades for different subjects.

using System;

namespace StudentGradesExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Declare and initialize a jagged array for student grades
            int[][] studentGrades = new int[3][]
            {
                new int[] { 85, 90 },
                new int[] { 78, 92, 88 },
                new int[] { 91, 82, 85, 87 }
            };

            // Display student grades
            for (int i = 0; i < studentGrades.Length; i++)
            {
                Console.Write($"Student {i + 1} Grades: ");
                for (int j = 0; j < studentGrades[i].Length; j++)
                {
                    Console.Write(studentGrades[i][j] + " ");
                }
                Console.WriteLine();
            }
        }
    }
}

Output

Student 1 Grades: 85 90
Student 2 Grades: 78 92 88
Student 3 Grades: 91 82 85 87

Benefits of Jagged Arrays

  1. Flexibility: Each sub-array can be of different lengths, which makes jagged arrays flexible for handling irregularly shaped data.
  2. Memory Efficiency: Jagged arrays can be more memory-efficient when dealing with uneven data because you only allocate memory for the elements you need.
  3. Ease of Use: Jagged arrays are simple to declare and use, especially when dealing with data that naturally fits into a nested structure.

Conclusion

Jagged arrays in C# provide a powerful and flexible way to handle collections of arrays with different lengths. By understanding how to declare, initialize, and manipulate jagged arrays, you can efficiently manage complex data structures that are not uniform. This capability is essential for developing robust and scalable applications that need to handle hierarchical or uneven data.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top