Programmingempire
Jagged arrays in C# are two-dimensional arrays with the number of columns not necessarily the same for each row. As opposed to the two-dimensional rectangular arrays, we can define the different sizes of each row in a jagged array. In this post on Creating Jagged Arrays in C#, I will explain jagged arrays and their implementation with a couple of examples.
Basically, a jagged array is an array of arrays. There are many situations when we find that using a normal rectangular array is not efficient. For instance, if in a two-dimensional array with a large number of columns, only a few values are non-zero and the rest of the values are zero, then it may result in wastage of a large amount of memory. In that case, using a jagged array is beneficial since it doesn’t allocate the whole memory at once.
In the case of a rectangular two-dimensional array, the memory for all columns in each row is allocated initially at the time of declaration. Whereas, in the case of a jagged array, the rows are allocated first. However, for allocating memory for columns in each row, a separate statement is required.
Rectangular Array vs. Jagged Array
A Two-Dimensional rectangular array has an equal number of columns in each row. While a 2X2 jagged array can have a different number of columns in each row. In addition, a single declaration statement creates a rectangular array as shown below. In contrast, the initial declaration statement allocates the space for only rows of a jagged array and the columns are created separately.
A jagged array can be an array of one-dimensional arrays. However, we can mix jagged array with multi-dimensional arrays where each element of the jagged array that indicates its rows, refers to a multi-dimensional array.
Jagged Arrays
Example of Creating Jagged Arrays in C#
The following code shows an example of a jagged array that contains 4 elements and each element, in turn, refers to another 1D array of a specified size. As shown in the output, the code displays the values that the user provides.
using System;
namespace ConsoleApp5
{
class Program
{
static void Main(string[] args)
{
int[][] jagged = new int[4][];
jagged[0] = new int[2];
jagged[1] = new int[5];
jagged[2] = new int[3];
jagged[3] = new int[4];
Console.WriteLine("Enter values in jagged array: ");
for(int i=0; i<4; i++)
{
Console.WriteLine("Row " + (i + 1));
for(int j=0; j<jagged[i].Length; j++)
{
jagged[i][j] = Int32.Parse(Console.ReadLine());
}
}
Console.WriteLine("Jagged Array: ");
for(int i=0;i<4;i++)
{
for(int j=0;j<jagged[i].Length;j++)
{
Console.Write(jagged[i][j] + " ");
}
Console.WriteLine();
}
}
}
}
Output:
Example:
In this example, there are two jagged arrays. The first one is an array of one-dimensional arrays and the second one is an array of 2D arrays. Also, the initialization statement assigns the values to the elements of arrays.
using System;
namespace ConsoleApp6
{
class Program
{
static void Main(string[] args)
{
int[][] j1 = new int[][]
{
new int[] {3,1,2,9},
new int[] {2,4,9},
new int[] {1,5,7, 12, 35, 80, 90}
};
Console.WriteLine("A jagged array with 3 rows. Each row contains a 1D array: ");
for(int i=0;i<=j1.GetUpperBound(0);i++)
{
for(int j=0;j<j1[i].Length;j++)
{
Console.Write(j1[i][j] + " ");
}
Console.WriteLine();
}
int[][,] j3 = new int[][,]
{
new int[,]{ {1, 2 }, {3, 4 }, {5, 6} },
new int[,]{ {7, 8 }, {9, 10 }, {11, 12 }, {13, 14 }, {15, 16 }, {17, 18 } },
new int[,]{ {19, 20 }, {21, 22 } },
new int[,]{ {23, 24 }, {25, 26 }, {27, 28 }, {29, 30 } }
};
Console.WriteLine("A Jagged array with four rows. Each row contains a 2D array:");
for (int i = 0; i < j3.Length; i++)
{
Console.WriteLine("Row " + (i + 1));
for (int j = 0; j <=j3[i].GetUpperBound(0); j++)
{
for(int k=0;k <= j3[i].GetUpperBound(1); k++)
Console.Write(j3[i][j, k] + " ");
Console.WriteLine();
}
Console.WriteLine();
}
}
}
}
Output:
Summary
Jagged arrays provide us a convinient way to store data in a multi-dimensional manner efficiently. Also, the jagged array allow us tp preserve the memory space and makes the execution faster.