InterviewSolution
Saved Bookmarks
| 1. |
What are Jagged Arrays in C#? |
|
Answer» Jagged arrays are arrays of arrays. The different arrays in a jagged array can be of many different sizes. The DECLARATION of elements in a jagged array are given as follows: int[ ][ ] jaggedArr = new int [5][ ]; In the above initialization, the jagged array is jaggedArr. There are 5 elements in the jagged array and each of these elements are 1-D integer arrays. A PROGRAM that demonstrates jagged arrays in C# is given as follows: using System; namespace Demo { class Example { STATIC void Main(string[] args) { int[][] jaggedArr = new int[5][]; jaggedArr[0] = new int[ ] { 8, 2, 5, 1 }; jaggedArr[1] = new int[ ] { 2, 4, 8 }; jaggedArr[2] = new int[ ] { 8, 4, 1, 9, 4}; jaggedArr[3] = new int[ ] { 7, 2 }; jaggedArr[4] = new int[ ] { 6, 1, 9, 5 }; for (int i = 0; i < jaggedArr.Length; i++) { System.Console.Write("Element {0}: ", i); for (int j = 0; j < jaggedArr[i].Length; j++) { System.Console.Write( jaggedArr[i][j] ); System.Console.Write(" "); } System.Console.WriteLine(); } } } }The OUTPUT of the above program is as follows: Element 0: 8 2 5 1 Element 1: 2 4 8 Element 2: 8 4 1 9 4 Element 3: 7 2 Element 4: 6 1 9 5 |
|