namespaceTwoDimensionalArayDemo { classProgram { staticvoidMain(string[] args) { //Assigning the array elements at the time of declaration int[,] arr = {{11,12,13,14}, {21,22,23,24}, {31,32,33,34}};
//printing values of array using for each loop foreach (int i in arr) { Console.Write(i + " "); } Console.WriteLine("\n");
//printing the values of array using nested for loop for (int i = 0; i < arr.GetLength(0); i++) { for (int j = 0; j < arr.GetLength(1); j++) { Console.Write(arr[i, j] + " "); } }
arr[0] = new int[5]; // we want five columns in the first row arr[1] = new int[6]; // we want six columns in the first row arr[2] = new int[4]; // we want four columns in the first row arr[3] = new int[5]; // we want five columns in the first row
namespaceTwoDimensionalArayDemo { classProgram { staticvoidMain(string[] args) { // Assigning the values of the jagged array // at the time of its declaration int[][] arr = { newint[4]{11,12,13,14}, newint[5]{21,22,23,24,25}, newint[3]{31,32,33} };
//printing the values of jagged array by using nested for loop for (int i = 0; i < arr.GetLength(0); i++) { for (int j = 0; j < arr[i].Length; j++) { Console.Write(arr[i][j] + " "); } } Console.WriteLine();
//print the values of jagged array by using foreach loop within for loop for (int i = 0; i < arr.GetLength(0); i++) { foreach (int x in arr[i]) { Console.Write(x + " "); } } Console.ReadKey(); } } }