Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What are the differences between a multi-dimensional array and jagged array?
Multi-dimensional arrays and jagged arrays are two different ways to work with nested data structures in C#. Understanding their differences is crucial for choosing the right approach for your specific use case.
Multi-dimensional Arrays
A multi-dimensional array is also called a rectangular array because all rows have the same number of columns, forming a rectangular structure. All sub-arrays must have identical lengths.
Syntax
Following is the syntax for declaring multi-dimensional arrays −
// 2D array int[,] array2D = new int[rows, columns]; // 3D array int[,,] array3D = new int[x, y, z];
Example
using System;
class Program {
public static void Main() {
// 2D multi-dimensional array
int[,] numbers = new int[3, 4] {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
Console.WriteLine("Multi-dimensional array:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
Console.Write(numbers[i, j] + " ");
}
Console.WriteLine();
}
Console.WriteLine("Array dimensions: " + numbers.GetLength(0) + "x" + numbers.GetLength(1));
}
}
The output of the above code is −
Multi-dimensional array: 1 2 3 4 5 6 7 8 9 10 11 12 Array dimensions: 3x4
Jagged Arrays
A jagged array is an array of arrays where each sub-array can have different lengths. It's called "jagged" because the edges are not uniform like a rectangular array.
Syntax
Following is the syntax for declaring jagged arrays −
// Declaration int[][] jaggedArray = new int[numberOfArrays][]; // Initialization of each sub-array jaggedArray[0] = new int[length1]; jaggedArray[1] = new int[length2];
Example
using System;
class Program {
public static void Main() {
// Jagged array with different row lengths
int[][] jaggedArray = new int[4][];
jaggedArray[0] = new int[2] {1, 2};
jaggedArray[1] = new int[4] {3, 4, 5, 6};
jaggedArray[2] = new int[3] {7, 8, 9};
jaggedArray[3] = new int[1] {10};
Console.WriteLine("Jagged array:");
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();
}
}
}
The output of the above code is −
Jagged array: Row 0: 1 2 Row 1: 3 4 5 6 Row 2: 7 8 9 Row 3: 10
Key Differences
| Multi-dimensional Array | Jagged Array |
|---|---|
| All sub-arrays have the same length (rectangular) | Sub-arrays can have different lengths |
Access: array[i,j]
|
Access: array[i][j]
|
| Memory allocated as single block | Each sub-array allocated separately |
| Better cache performance | More flexible structure |
| Fixed size at declaration | Sub-arrays can be resized |
Memory Usage Comparison
using System;
class Program {
public static void Main() {
// Multi-dimensional array - fixed rectangular structure
int[,] multiArray = new int[3, 4];
// Jagged array - flexible structure
int[][] jaggedArray = new int[3][];
jaggedArray[0] = new int[2]; // First row: 2 elements
jaggedArray[1] = new int[4]; // Second row: 4 elements
jaggedArray[2] = new int[1]; // Third row: 1 element
Console.WriteLine("Multi-dimensional array total elements: " + multiArray.Length);
int jaggedTotal = 0;
for (int i = 0; i < jaggedArray.Length; i++) {
jaggedTotal += jaggedArray[i].Length;
}
Console.WriteLine("Jagged array total elements: " + jaggedTotal);
}
}
The output of the above code is −
Multi-dimensional array total elements: 12 Jagged array total elements: 7
Conclusion
Multi-dimensional arrays provide a rectangular structure with uniform row lengths and better memory performance, while jagged arrays offer flexibility with variable row lengths. Choose multi-dimensional arrays for matrix operations and jagged arrays when you need different sized sub-arrays.
