Jagged arrays in C# are arrays of arrays, where each element can have a different length. They offer flexibility for handling irregular data structures and dynamic datasets.
To declare a jagged array, use square brackets for each dimension. After declaring the jagged array, initialize each inner array separately. For example:
int[][] jaggedArray = new int[3][]; jaggedArray[0] = new int[] { 1, 2, 3 }; jaggedArray[1] = new int[] { 4, 5 }; jaggedArray[2] = new int[] { 6, 7, 8, 9 };
You can access individual elements of a jagged array using multiple index values. For example:
int element = jaggedArray[1][1]; // Accesses the element at row 1 and column 1
Here is source code of the C# Program to Demonstrate Jagged Arrays. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.
/* * C# Program to Demonstrate Jagged Arrays */ using System; class Program { static void Main() { int[][] jag = new int[3][]; jag[0] = new int[2]; jag[0][0] = 11; jag[0][1] = 12; jag[1] = new int[1] {11}; jag[2] = new int[3] { 14,15, 16 }; for (int i = 0; i < jag.Length; i++) { int[] innerArray = jag[i]; for (int a = 0; a < innerArray.Length; a++) { Console.WriteLine(innerArray[a] + " "); } } Console.Read(); } }
1. This C# program demonstrates Jagged Arrays.
2. The elements in a jagged array can have different dimensions and sizes, also known as an “array of arrays“.
3. Create a new array in the jagged array, assign its values, and form the second and third rows using array initialization.
4. Jagged Arrays efficiently store multiple rows of varying lengths with any data type. A for loop prints the jagged array elements.
11 12 11 14 15 16
- Flexibility: Elements can have different lengths.
- Memory Efficiency: Jagged arrays use memory efficiently as they allocate memory only for the individual arrays.
- Handling Irregular Data: Ideal for managing irregular data, such as a list of variable-length sentences or words.
- Hierarchical Data: Suitable for representing and manipulating hierarchical data structures.
- Versatility: Works well with matrices of varying column sizes.
This C# program calculates and displays the length of each row in the jagged array using predefined functions.
Here is source code of the C# Program to Find the Length of the Jagged Array using Predefined Functions. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.
/* * C# Program to Find the Length of the Jagged Array using Predefined Functions */ using System; class Program { public static void Main() { byte[][] numbers = new byte[5][]; for (int i = 0; i < numbers.Length; i++) { numbers[i] = new byte[i + 3]; } for (int i = 0; i < numbers.Length; i++) { Console.WriteLine("Length of row {0} is {1}", i, numbers[i].Length); } Console.Read(); } }
1. Declare a jagged array numbers with five inner arrays (rows).
2. Initialize the inner arrays with varying lengths using a for loop, where each row’s length is i + 3.
3. The first inner array (row 0) has a length of 3, the second (row 1) has 4, and so on.
4. Use another for loop to iterate through the numbers array and print the length of each row using Console.WriteLine.
Length of row 0 is 3 Length of row 1 is 4 Length of row 2 is 5 Length of row 3 is 6 Length of row 4 is 7
Sanfoundry Global Education & Learning Series – 1000 C# Programs.
- Get Free Certificate of Merit in C# Programming
- Participate in C# Programming Certification Contest
- Become a Top Ranker in C# Programming
- Take C# Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Buy Computer Science Books
- Practice Computer Science MCQs
- Practice MCA MCQs
- Apply for C# Internship
- Buy C# Books