In C#, jagged arrays offer a unique way to structure data by allowing arrays of arrays, providing more flexibility than multi-dimensional arrays. Unlike multi-dimensional arrays, jagged arrays allow each sub-array to have a different length. This is especially useful in scenarios where data needs a variable structure, such as seating arrangements in theaters or lists of students with varying numbers of subjects.
A jagged array in C# is an array where each element is itself an array, which can have varying lengths. This type of array is often referred to as an "array of arrays" and is represented with the syntax type[][]
. Unlike multi-dimensional arrays, which require a fixed structure, jagged arrays provide a flexible way to handle collections of collections with varied lengths.
Jagged arrays are beneficial when:
Jagged arrays are declared using a series of square brackets, where each set of brackets denotes a new level in the array hierarchy. Initialization requires explicitly creating each inner array.
Here’s how to declare a jagged array with two levels:
int[][] jaggedArray;
To initialize a jagged array, each element (which is itself an array) must be individually allocated.
int[][] jaggedArray = new int[3][]; // Declares a jagged array with 3 inner arrays
// Initializing each inner array with different lengths
jaggedArray[0] = new int[2]; // First array has 2 elements
jaggedArray[1] = new int[3]; // Second array has 3 elements
jaggedArray[2] = new int[4]; // Third array has 4 elements
You can also initialize jagged arrays with values directly:
int[][] jaggedArray = {
new int[] {1, 2},
new int[] {3, 4, 5},
new int[] {6, 7, 8, 9}
};
Elements in a jagged array are accessed by using multiple indices, where the first index selects the sub-array, and the second index selects the element within that sub-array.
Console.WriteLine(jaggedArray[0][1]); // Output: 2
jaggedArray[1][2] = 10; // Changes the third element of the second sub-array to 10
To iterate over a jagged array, you can use nested loops:
for (int i = 0; i < jaggedArray.Length; i++)
{
for (int j = 0; j < jaggedArray[i].Length; j++)
{
Console.Write(jaggedArray[i][j] + " ");
}
Console.WriteLine();
}
Consider a scenario where each classroom has a different number of students, and each student has a different number of scores for a variable number of tests. This setup is ideal for a jagged array since each student’s score list can vary in length.
Using a jagged array, we can store each classroom’s students as sub-arrays, where each student’s scores are stored in arrays of different lengths.
class ClassroomScores
{
static void Main(string[] args)
{
// Declare and initialize a jagged array where each classroom has a different number of students and test scores
int[][] classroomScores = {
new int[] {85, 92, 88}, // Scores for student 1 in classroom 1
new int[] {78, 74}, // Scores for student 2 in classroom 1
new int[] {91, 89, 95, 87} // Scores for student 3 in classroom 1
};
// Calculate average scores for each student
for (int i = 0; i < classroomScores.Length; i++)
{
int total = 0;
for (int j = 0; j < classroomScores[i].Length; j++)
{
total += classroomScores[i][j];
}
double average = (double)total / classroomScores[i].Length;
Console.WriteLine($"Average score for student {i + 1}: {average:F2}");
}
}
}
classroomScores
as a jagged array, where each student’s test scores are stored in sub-arrays of different lengths.total
and average
score for each student.Average score for student 1: 88.33
Average score for student 2: 76.00
Average score for student 3: 90.50
Jagged arrays in C# provide a versatile solution for storing collections of arrays with varying sizes. Unlike multi-dimensional arrays, they allow each sub-array to be of a different length, which is particularly helpful in scenarios where data is irregular or hierarchical. This guide has covered the basics of declaring, initializing, accessing, and modifying jagged arrays, along with practical examples to illustrate their real-world applications. By understanding and applying jagged arrays, developers can efficiently manage complex data structures, making them a valuable tool in C#.