Jagged Array:
A jagged array is an array whose elements are arrays. The elements of a jagged array can be of different dimensions and sizes. A jagged array is sometimes called an "array of arrays."
Jagged Arrays are a Special Type of Arrays that can be used to store rows of data having different length to improve performance when working with Multi-Dimensional Arrays:
Declaring Jagged Arrays:
When Declaring a Jagged Array, We can just declare the Number of rows of the Array and prefer to specify the Number of Columns at Run-time.
//Declaring Jagged Arrays:
Data-Type [][] Variable-Name = new Data-Type [5][];
Initializing Jagged Array.
We can Initialize the Jagged Array upon Declaration like this:
int[][] JagArray = new int[][]
{
new int[] {1, 2, 3},
new int[] {4, 5, 6, 7},
new int[] {8, 9, 10, 11, 12, 13},
new int[] {14, 15, 16}
};
Or We can Initialize the Jagged Array using this shorthand form too.
int[][] JagArray =
{
new int[] {1, 2, 3},
new int[] {4, 5, 6, 7},
new int[] {8, 9, 10, 11, 12, 13},
new int[] {14, 15, 16}
};
Jagged Array Example:
let's have an Example to know how exactly we can declare and initialize the Jagged array. and how this jagged array work in C#.
/*Example: Jagged Array : InfoBrother:*/
using System;
namespace ArrayApplication
{
class JaggedArray
{
static void Main(string[] args)
{
/* a jagged array of 5 array of integers*/
int[][] jagAray = new int[][]
{
new int[] { 0, 1, 2},
new int[] { 3, 4, 5},
new int[] { 6, 7, 8},
new int[] { 9, 10, 11},
new int[] { 12, 13, 14}
};
/* output each array element's value */
for (int x=0; x < 5; x++) //for rows (x):
{
for (int y=0; y < 3; y++) //for columns (y):
{
Console.WriteLine("jagAray[{0}][{1}] = {2}", x, y, jagAray[x][y]);
}
}
Console.ReadKey();
}
}
}
Jagged Arrays:
jagAray[0][0] = 0 jagAray[0][1] = 1 jagAray[0][2] = 2 jagAray[1][0] = 3 jagAray[1][1] = 4 jagAray[1][2] = 5 jagAray[2][0] = 6 jagAray[2][1] = 7 jagAray[2][2] = 8 jagAray[3][0] = 9 jagAray[3][1] = 10 jagAray[3][2] = 11 jagAray[4][0] = 12 jagAray[4][1] = 13 jagAray[4][2] = 14
Let's have another Example of Jagged Arrays. in This example, we will declared five rows in the Array that will contain five string arrays of Different Lengths. we will try to show some Programming Language in This example using Jagged Array:
/*Example : Jagged Array: InfoBrother:*/
using System;
namespace jagged_array
{
class Program
{
static void Main(string[] args)
{
string[][] Language = new string[4][]
{
new string[]{"C++", "Csharp", "Java"},
new string[]{"JavaScript", "PHP", "HTML", "CSS", "JQuery"},
new string[]{"Python", "Ruby", "SQL", "SVG", "Pascal", "VBScript"},
new string[]{"Perl", "Lua", "Pascal", "ASP.NET", "C", "AppMl", "Bootstrap"},
};
for (int x = 0; x < Language.Length; x++) //for rows:
{
System.Console.Write("Language list: ({0}): ", x + 1);
for (int y = 0; y < Language[x].Length; y++) //for Columns:
{
System.Console.Write(Language[x][y] + "\t");
}
System.Console.WriteLine();
}
Console.ReadKey();
}
}
}
Jagged Arrays:
Language list: (1): C++ Csharp Java Language list: (2): JavaScript PHP HTML CSS JQuery Language list: (3): Python Ruby SQL SVG Pascal VBScript Language list: (4): Perl Lua Pascal ASP.NET C AppMl Bootstrap
Jagged Array With Multi-Dimensional Arrays:
It is possible to mix jagged and multidimensional arrays. The following is a declaration and initialization of a single-dimensional jagged array that contains two-dimensional array elements of different sizes:
int[][,] jaggedArray = new int[5][,]
{
new int[,] { {1, 2}, {3, 4} },
new int[,] { {5, 6}, {7, 8}, {9, 10} },
new int[,] { {11, 12}, {12, 14} },
new int[,] { {15, 16}, {17, 18}, {19, 20} },
new int[,] { {21, 22}, {23, 24}, {25, 26} }
};