Jagged Arrays in c#
- C# arrays has one special feature i.e jagged arrays.
- An array whose elements are arrays i.e Arrays of arrays.
- These array elements has different dimensions and sizes.
- This program is example single-dimensional array.
// Main Method
public static void Main()
{
/*----------2D Array---------------*/
// Declare the array of two elements:
int[][] arr = new int[2][];
// Initialize the elements:
arr[0] = new int[5] { 1, 3, 5, 7, 9 };
arr[1] = new int[4] { 2, 4, 6, 8 };
// Another way of Declare and
// Initialize of elements
int[][] arr1 = { new int[] { 1, 3, 5, 7, 9 },
new int[] { 2, 4, 6, 8 } };
// Display the array elements:
for (int i = 0; i < arr.Length; i++)
{
System.Console.Write("Element [" + i + "] Array: ");
for (int j = 0; j < arr[i].Length; j++)
Console.Write(arr[i][j] + " ");
Console.WriteLine();
}
Console.WriteLine("Another Array");
// Display the another array elements:
for (int i = 0; i < arr1.Length; i++)
{
System.Console.Write("Element [" + i + "] Array: ");
for (int j = 0; j < arr1[i].Length; j++)
Console.Write(arr1[i][j] + " ");
Console.WriteLine();
}
Console.ReadLine();
}
No comments