xxxxxxxxxx
// C# program to illustrate the declaration
// and Initialization of Jagged Arrays
using System;
class GFG {
// Main Method
public static void Main()
{
// Declare the Jagged Array of four elements:
int[][] jagged_arr = new int[4][];
// Initialize the elements
jagged_arr[0] = new int[] {1, 2, 3, 4};
jagged_arr[1] = new int[] {11, 34, 67};
jagged_arr[2] = new int[] {89, 23};
jagged_arr[3] = new int[] {0, 45, 78, 53, 99};
// Display the array elements:
for (int n = 0; n < jagged_arr.Length; n++) {
// Print the row number
System.Console.Write("Row({0}): ", n);
for (int k = 0; k < jagged_arr[n].Length; k++) {
// Print the elements in the row
System.Console.Write("{0} ", jagged_arr[n][k]);
}
System.Console.WriteLine();
}
}
}
Jagged array is a array of arrays such that member arrays can be of different sizes. In other words, the length of each array index can differ. The elements of Jagged Array are reference types and initialized to null by default. Jagged Array can also be mixed with multidimensional arrays. Here, the number of rows will be fixed at the declaration time, but you can vary the number of columns.
xxxxxxxxxx
// In C#, a jagged array consists of multiple arrays as its element
//Syntax:
//dataType[ ][ ] nameOfArray = new dataType[rows][ ];
using System;
namespace JaggedArray {
class Program {
static void Main(string[] args) {
// create a jagged array
int[ ][ ] jaggedArray = {
new int[] {1, 3, 5},
new int[] {2, 4},
};
// print elements of jagged array
Console.WriteLine("jaggedArray[1][0]: " + jaggedArray[1][0]);
Console.WriteLine("jaggedArray[1][1]: " + jaggedArray[1][1]);
Console.WriteLine("jaggedArray[0][2]: " + jaggedArray[0][2]);
Console.ReadLine();
}
}
}
xxxxxxxxxx
jagged array is also known as varriable size array. Can store data of any size.
int[][] jagged_arr = new int[3][];
jagged_arr[0] = new int[] {1, 7, 9, 22};
jagged_arr[1] = new int[] {11, 34, 67};
jagged_arr[2] = new int[] {110, 24};