Jagged Arrays in C#
A jagged array is an array of arrays, where each element can have a different length. This flexibility allows you to create arrays with varying dimensions.
int[][] jaggedArray = new int[3][]; // Declares a jagged array with 3 rows
// Initialize each row with different lengths
jaggedArray[0] = new int[3]; // First row with 3 elements
jaggedArray[1] = new int[2]; // Second row with 2 elements
jaggedArray[2] = new int[4]; // Third row with 4 elements
// Assign values to the elements
jaggedArray[0][0] = 1;
jaggedArray[0][1] = 2;
jaggedArray[0][2] = 3;
// ... and so on
//another way
int[][] jaggedArray = new int[3][];
jaggedArray[0] = new int[] { 1, 2, 3 };
jaggedArray[1] = new int[] { 4, 5 };
jaggedArray[2] = new int[] { 6, 7, 8, 9 };
//Accessing Elements:
int element = jaggedArray[1][0]; // Accesses the element at row 1, column 0
Simple Example:
int[][] jaggedArray = new int[3][];
jaggedArray[0] = new int[] { 10, 20, 30 };
jaggedArray[1] = new int[] { 40, 50 };
jaggedArray[2] = new int[] { 60, 70, 80, 90 };
// Calculate the sum of all elements
int sum = 0;
for (int i = 0; i < jaggedArray.Length; i++)
{
for (int j = 0; j < jaggedArray[i].Length; j++)
{
sum += jaggedArray[i][j];
}
}
Console.WriteLine("Sum of all elements: " + sum);
Key Points:
Each row in a jagged array can have a different number of columns.
Jagged arrays can be useful for representing hierarchical data structures or matrices with varying dimensions.
While flexible, jagged arrays can be more complex to work with compared to regular multidimensional arrays.
When to Use Jagged Arrays:
Irregular Data Structures: When you need to represent data that doesn't fit into a regular, rectangular structure.
Hierarchical Data: For hierarchical data, like a tree structure or a file system.
Dynamic Data: When you don't know the exact dimensions of the data beforehand.
Example: Representing a Tree Structure
int[][] tree = new int[3][];
tree[0] = new int[] { 1 };
tree[1] = new int[] { 2, 3 };
tree[2] = new int[] { 4, 5, 6 };
This jagged array can represent a tree structure with a root node (1), two child nodes (2 and 3), and three leaf nodes (4, 5, and 6).
Feature | Jagged Array 🧩 | Multi-Dimensional Array 🏢 |
Definition | An array of arrays, where each element can be an array of a different size. | A tabular collection of elements organized in rows and columns. |
Declaration | int[][] jaggedArray = new int[3][]; | int[,] matrix = new int[3, 4]; |
Flexibility | More flexible, as each row can have a different number of elements. | Less flexible, all rows must have the same number of elements. |
Memory Usage | Can be more memory-efficient in some cases, as you only allocate memory for the necessary elements. | Less memory-efficient, as you allocate memory for all elements, even if some are unused. |
Complexity | More complex to work with, as you need to handle varying row lengths. | Simpler to work with, as all rows have the same structure. |
Use Cases | Representing hierarchical data, like tree structures or file systems. | Representing matrices, tables, or grids, like game boards, image pixels, etc. |