Link Search Menu Expand Document

C# Jagged Arrays

A C# jagged array is a specific type of multidimensional arrays where the numbers of items in different rows are not equal. In this article, you will see how to create C# jagged arrays, how to add and update items in C# jagged arrays, and how to print C# jagged arrays. So, let’s begin without ado.

Table of Contents

  1. Creating a C# Jagged Array
  2. Adding and Updating Items in C# Jagged Arrays
  3. Printing Items from C# Jagged Arrays

Creating a C# Jagged Array

You can define a C# jagged array like any other multidimensional array. It is recommended that while defining a jagged array, you should only pass the integer value that specifies the number of rows in a multidimensional array. Since the number of items in each row can be different, therefore the number of items for an individual row can be defined while adding the row.

The following script creates a simple jagged array called jagged_array. The jagged array can store 5 sub-arrays. In the first method, you simply define the array and the number of rows that the jagged array will contain. In the second method, 5 rows are added at the time of definition of the jagged array. While the third method in the following script shows how you can add a jagged array using a shorthand notation.

using System;

namespace DariaApp
{

class Program
{
static void Main(string[] args)
{
// Method 1
int[][] jagged_array = new int[5][];

//Method 2
int[][] jagged_array2 = new int[][]
{
new int[] {1, 5, 10, 8},
new int[] {12, 53},
new int[] {20, 45, 32, 16},
new int[] {1, 2, 3, 4, 5, 6},
new int[] {4, 6}
};

// Method 3

int[][] jagged_array3 = 
{
new int[] {1, 5, 10, 8},
new int[] {12, 53},
new int[] {20, 45, 32, 16},
new int[] {1, 2, 3, 4, 5, 6},
new int[] {4, 6}
};

Console.ReadKey();
}

}
}

Adding and Updating Items in C# Jagged Arrays

Jagged arrays, like other C# arrays, follow the zero-based index scheme for storing elements. Hence, the first item is stored at the 0th index of an array. Similarly, in the case of a multidimensional array, the first array is stored at the 0th index. Here is an example.

In the following script, you define a jagged array with 5 rows. Next, you store 5 arrays individually in 0 to 4th index of the jagged array. To access an index of an array, you simply have to pass the index number in square brackets that follow the array name. To add or update an index, you simply equate an item or an array to the index of the jagged array.

using System;

namespace DariaApp
{

class Program
{
static void Main(string[] args)
{

int[][] jagged_array = new int[5][];

// adding an item
jagged_array[0] = new int[] { 1, 5, 10, 8 };
jagged_array[1] = new int[] { 12, 53 };
jagged_array[2] = new int[] { 20, 45, 32, 16 };
jagged_array[3] = new int[] { 1, 2, 3, 4, 5, 6 };
jagged_array[4] = new int[] { 4, 6 };

// updating an item

jagged_array[0] = new int[] { 20,45,60 };
jagged_array[1] = new int[] { 20, 45 };

Console.ReadKey();
} 
}
}

Printing Items from C# Jagged Arrays

To print the items from a jagged array, you again have to access the indexes of the jagged array. For instance, to access the array at the 0th index of the jagged array, you can use the syntax jagged_array[0]. In the following script, you first create a jagged array with 5 rows. Next, you first print the item at the 1st index of the array at the 1st index using the syntax jagged_array[1][1]. After that, you employ a C# foreach loop to iterate through all the items in the array at index 2.

using System;

namespace DariaApp
{

class Program
{
static void Main(string[] args)
{

int[][] jagged_array = new int[5][];

// adding an item
jagged_array[0] = new int[] { 1, 5, 10, 8 };
jagged_array[1] = new int[] { 12, 53 };
jagged_array[2] = new int[] { 20, 45, 32, 16 };
jagged_array[3] = new int[] { 1, 2, 3, 4, 5, 6 };
jagged_array[4] = new int[] { 4, 6 };


// Access the item in first index of row at first index 
Console.WriteLine(jagged_array[1][1]);

// Access all the items of the array at index 2
foreach(int item in jagged_array[2])
{
Console.Write(item + ", ");
}

Console.ReadKey();
} 
}
}

Output:

C Jagged Arrays

 

You can also iterate through all the items in all the arrays in a jagged array. To do so, you have to execute two foreach loops. The outer foreach loop iterates through all the arrays. The inner foreach loop iterates through each item in an array and prints it on the console. The following script prints all the items in a jagged array.

using System;

namespace DariaApp
{

class Program
{
static void Main(string[] args)
{

int[][] jagged_array = new int[5][];

// adding an item
jagged_array[0] = new int[] { 1, 5, 10, 8 };
jagged_array[1] = new int[] { 12, 53 };
jagged_array[2] = new int[] { 20, 45, 32, 16 };
jagged_array[3] = new int[] { 1, 2, 3, 4, 5, 6 };
jagged_array[4] = new int[] { 4, 6 };


foreach (int[] array in jagged_array)
{
foreach (int item in array)
{
Console.Write(item + ", ");
}
Console.WriteLine();
}

Console.ReadKey();
} 
}
}

Output:

C Sharp Jagged Arrays

Other useful articles:


Back to top

© , C Sharp Online — All Rights Reserved - Terms of Use - Privacy Policy