Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c#
arrays
data structures

What is a jagged array in C#?

Salman Masood

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Jagged arrays

In C#, jagged arrays are arrays whose elements themselves are arrays. The arrays defined inside the elements are not bound to fix size. In simple terms, the length of each array can differ.

Syntax

The following is the syntax of a jagged array declaration:

dataType [] [] arrayName = new dataType[no. of rows] []

Example

int [] [] jaggedArray = new int[6] []

While declaration, the rows of jagged arrays are fixed in size, as shown above.

Once the array is declared, it needs to be initialized as follows:

int[][] jaggedArray = new int [4][] //array declaration
jaggedArray[0] = new int[2];  
jaggedArray[1] = new int[4];  
jaggedArray[2] = new int[6];  
jaggedArray[3] = new int[8]; //size of each element array defined
jaggedArray[0] = new int[] { 3, 5};// population of jagged array
jaggedArray[1] = new int[] { 1, 2, 3, 4 };  
jaggedArray[2] = new int[] { 5, 6, 7, 8, 9, 10 };  
jaggedArray[3] = new int[] { 11, 12, 13, 14, 15, 16, 67, 24 };
 

The array can also be initialized and declared at the same time as follows:

int[][] jaggedArray = new int[][]  
{  
new int[] { 3, 4, 5 },  
new int[] { 1, 2, 4, 8, 16 },  
new int[] { 1, 6 },  
new int[] { 100, 20, 29, 45, 63, 45, 67, 78 }  
};

Example

Following is the code for declaring, initializing, and printing the contents of the jagged array.

using System;
namespace jagged_array
{
class Program
{
static void Main(string[] args)
{
int [][] jaggedArr = new int[3][]; //array declaration
jaggedArr[0] = new int[2] {2, 4}; //jagged array initialization and population
jaggedArr[1] = new int[4] {45, 25, 23, 65};
jaggedArr[2] = new int[3] {23, 67, 52};
for(int i = 0; i < jaggedArr.Length; i++) // outer for loop to traverse through each elemment of array
{
System.Console.Write("Element({0})", i + 1);
for(int j = 0; j< jaggedArr[i].Length; j++) // inner for loop to ouput the contents of jagged array
{
System.Console.Write(jaggedArr[i][j] + "\t"); // printing results of jagged array
}
System.Console.WriteLine();
}
}
}
}

Explanation

  • Line 8: We declare the jagged array containing three elements.
  • Lines 10–13: We initialize and populate the elements of the array. In this case, each element itself defines an array.
  • Line 15: We use the outer for loop to get the elements of the main array.
  • Line 18: We use the inner for loop to get the array within the array.
  • Line 20: We print the contents of the array.

RELATED TAGS

c#
arrays
data structures

CONTRIBUTOR

Salman Masood
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring