Jagged Array in C#
What is a Jagged Array?
A Jagged Array is an array of arrays. Each element of the
main array is another array.
A jagged array is an array whose elements are arrays.
Unlike a multidimensional array,
each row can have a different number of elements.
Simple Diagram
%22%20viewBox%3D%220%200%20340%20170%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%3E%3Crect%20x%3D%2210%22%20y%3D%2220%22%20width%3D%2270%22%20height%3D%22120%22%20rx%3D%226%22%20fill%3D%22none%22%20stroke%3D%22currentColor%22%20stroke-width%3D%222%22%2F%3E%3Ctext%20x%3D%2215%22%20y%3D%2215%22%20font-size%3D%2212%22%20fill%3D%22currentColor%22%3EMain%20Array%3C%2Ftext%3E%3Ctext%20x%3D%2222%22%20y%3D%2245%22%20font-size%3D%2212%22%20fill%3D%22currentColor%22%3ERow%200%3C%2Ftext%3E%3Ctext%20x%3D%2222%22%20y%3D%2280%22%20font-size%3D%2212%22%20fill%3D%22currentColor%22%3ERow%201%3C%2Ftext%3E%3Ctext%20x%3D%2222%22%20y%3D%22115%22%20font-size%3D%2212%22%20fill%3D%22currentColor%22%3ERow%202%3C%2Ftext%3E%3Cpath%20d%3D%22M80%2040%20L100%2040%22%20stroke%3D%22currentColor%22%20stroke-width%3D%222%22%2F%3E%3Cpath%20d%3D%22M80%2075%20L100%2075%22%20stroke%3D%22currentColor%22%20stroke-width%3D%222%22%2F%3E%3Cpath%20d%3D%22M80%20110%20L100%20110%22%20stroke%3D%22currentColor%22%20stroke-width%3D%222%22%2F%3E%3Crect%20x%3D%22100%22%20y%3D%2225%22%20width%3D%2228%22%20height%3D%2228%22%20fill%3D%22none%22%20stroke%3D%22currentColor%22%2F%3E%3Crect%20x%3D%22132%22%20y%3D%2225%22%20width%3D%2228%22%20height%3D%2228%22%20fill%3D%22none%22%20stroke%3D%22currentColor%22%2F%3E%3Crect%20x%3D%22100%22%20y%3D%2260%22%20width%3D%2228%22%20height%3D%2228%22%20fill%3D%22none%22%20stroke%3D%22currentColor%22%2F%3E%3Crect%20x%3D%22132%22%20y%3D%2260%22%20width%3D%2228%22%20height%3D%2228%22%20fill%3D%22none%22%20stroke%3D%22currentColor%22%2F%3E%3Crect%20x%3D%22164%22%20y%3D%2260%22%20width%3D%2228%22%20height%3D%2228%22%20fill%3D%22none%22%20stroke%3D%22currentColor%22%2F%3E%3Crect%20x%3D%22196%22%20y%3D%2260%22%20width%3D%2228%22%20height%3D%2228%22%20fill%3D%22none%22%20stroke%3D%22currentColor%22%2F%3E%3Crect%20x%3D%22100%22%20y%3D%2295%22%20width%3D%2228%22%20height%3D%2228%22%20fill%3D%22none%22%20stroke%3D%22currentColor%22%2F%3E%3Crect%20x%3D%22132%22%20y%3D%2295%22%20width%3D%2228%22%20height%3D%2228%22%20fill%3D%22none%22%20stroke%3D%22currentColor%22%2F%3E%3Crect%20x%3D%22164%22%20y%3D%2295%22%20width%3D%2228%22%20height%3D%2228%22%20fill%3D%22none%22%20stroke%3D%22currentColor%22%2F%3E%3Ctext%20x%3D%22110%22%20y%3D%2243%22%20font-size%3D%2211%22%20fill%3D%22currentColor%22%3E1%3C%2Ftext%3E%3Ctext%20x%3D%22142%22%20y%3D%2243%22%20font-size%3D%2211%22%20fill%3D%22currentColor%22%3E2%3C%2Ftext%3E%3Ctext%20x%3D%22110%22%20y%3D%2278%22%20font-size%3D%2211%22%20fill%3D%22currentColor%22%3E3%3C%2Ftext%3E%3Ctext%20x%3D%22142%22%20y%3D%2278%22%20font-size%3D%2211%22%20fill%3D%22currentColor%22%3E4%3C%2Ftext%3E%3Ctext%20x%3D%22174%22%20y%3D%2278%22%20font-size%3D%2211%22%20fill%3D%22currentColor%22%3E5%3C%2Ftext%3E%3Ctext%20x%3D%22206%22%20y%3D%2278%22%20font-size%3D%2211%22%20fill%3D%22currentColor%22%3E6%3C%2Ftext%3E%3Ctext%20x%3D%22110%22%20y%3D%22113%22%20font-size%3D%2211%22%20fill%3D%22currentColor%22%3E7%3C%2Ftext%3E%3Ctext%20x%3D%22142%22%20y%3D%22113%22%20font-size%3D%2211%22%20fill%3D%22currentColor%22%3E8%3C%2Ftext%3E%3Ctext%20x%3D%22174%22%20y%3D%22113%22%20font-size%3D%2211%22%20fill%3D%22currentColor%22%3E9%3C%2Ftext%3E%3C%2Fsvg%3E)
Each row contains a different number of elements.
Features of Jagged Array
It is an array of arrays.
Each row can have a different length.
It is useful for storing uneven or irregular data.
It uses memory efficiently because each row is created only with the required size.
Declaration
A jagged array is declared using two square brackets ([][]).
int[][] jaggedArr = new int[3][];
Explanation
int[][] → Jagged array of integers.
new int[3][] → Creates a main array with 3 rows.
The rows are created, but their sizes are not assigned yet.
Initializing a Jagged Array
Method 1: Initialize Row by Row
int[][] jaggedArr = new int[3][];
jaggedArr[0] = new int[2];
jaggedArr[1] = new int[4];
jaggedArr[2] = new int[3];
Structure
Row
Number of Elements
Row 0
2
Row 1
4
Row 2
3
Each row has its own size.
Accessing Elements
Elements are accessed using two indexes.
Syntax
jaggedArr[row][column]
Examples
jaggedArr[0][0]; // 1
jaggedArr[1][2]; // 5
jaggedArr[2][1]; // 8
Meaning
First index (row) selects the row.
Second index (column) selects the element inside that row.
Traversing a Jagged Array
A jagged array is traversed using nested loops.
The outer loop visits each row.
The inner loop visits each element in the selected row.
complete Example Program
using System;
class JaggedArray
{
static void Main()
{
int[][] jaggedArr = new int[4][];
jaggedArr[0] = new int[] {1, 2, 3, 4};
jaggedArr[1] = new int[] {11, 34, 67};
jaggedArr[2] = new int[] {89, 23};
jaggedArr[3] = new int[] {0, 45, 78, 53, 99};
for (int i = 0; i < jaggedArr.Length; i++)
{
Console.Write("Row " + i + ": ");
for (int j = 0; j < jaggedArr[i].Length; j++)
{
Console.Write(jaggedArr[i][j] + " ");
}
Console.WriteLine();
}
}
}
Output
Row 0: 1 2 3 4
Row 1: 11 34 67
Row 2: 89 23
Row 3: 0 45 78 53 99
Jagged Array vs Multidimensional Array
Jagged Array
Multidimensional Array
Array of arrays.
Single array with rows and
columns.
Rows can have different lengths.
All rows have the same
number of columns.
Declared as int[][].
Declared as int[,].
Flexible for irregular data.
Suitable for regular
table-like data.
Param Array in C#
A Param Array is a method parameter declared using the params keyword. It allows a method to accept a variable number of arguments of the same data type.
Definition: A param array is a special parameter that lets a method receive zero, one, or many values without creating an array separately.
Syntax
returnType MethodName(params dataType[] parameterName)
{
// Method body
}Explanation
params→ Keyword used for variable arguments.dataType[]→ Must be a single-dimensional array.parameterName→ Name of the parameter array.
How Param Array Works
When a method is called, all the values passed are automatically stored in an array.
Visual Diagram
Method Call
Sum(10, 20, 30);Inside the Method
arr[] = {10, 20, 30}Example Program
using System;
class Program
{
static int Sum(params int[] numbers)
{
int total = 0;
foreach (int num in numbers)
{
total += num;
}
return total;
}
static void Main()
{
Console.WriteLine(Sum(10, 20, 30));
Console.WriteLine(Sum(5, 15));
Console.WriteLine(Sum());
}
}
Comments
Post a Comment