Jagged Arrays

A Jagged array is an array of arrays. The jagged array provides us the flexibility and facility to create different size of rows.

In the previous section as you have seen that the two-dimensional arrays look like a rectangular and they have a fixed size for each row. While Jagged arrays can have different size for each row. Let's look at this with an example.

The above example is showing two images, the first image is of a two-dimensional array which contains four rows of the same size. The second image is showing a jagged array which has four rows as well but they are different in size.

Declaration

By using two opening and closing brackets one after another we can declare the jagged arrays. Following are the different ways to declare a jagged array. While doing the initialization of a jagged array, we provide the total number of arrays in the first bracket after equals operator.

int[][] jaggedArray = new int[2][];  //An integer jagged array which can have 2 rows/arrays.
string[][] jaggedArrayStr = new string[5][];  //String jagged array which can have 5 rows/arrays.

Initialization

//By providing the size of array. If the size is given then that number of elements must be present inside two curly braces
jaggedArray[0] = new int[5] { 1, 4, 5, 6, 7 };
jaggedArray[1] = new int[3] { 1, 2, 3 };
 
//If the array size is not defined then total number of elements inside two curly braces determines the array size
jaggedArray[0] = new int[] { 1, 2, 3, 4, 5, 6, 7 }; //Array size 7
jaggedArray[1] = new int[] { 1, 2, 3 };//Array size 3

By using two nested for loops we can traverse through a jagged array. The outer loop iterates over the total number of rows. Whereas the second loop iterates over the number of elements inside a row.

Traversing the Jagged Array

Example

Try It
using System;
 
namespace TLExamples
{
    class Program
    {
        public static void Main()
        {
            string[][] players = new string[3][];
 
 
            players[0] = new string[4] { "Cricket", "Hocky", "Rugby", "Badminton" };
            players[1] = new string[3] { "Hocky", "Rugby", "Badminton" }; ;
            players[2] = new string[5] { "Cricket", "Hocky", "Rugby", "Badminton", "Football" };
 
 
            players[0] = new string[] { "Cricket", "Hocky", "Rugby", "Badminton" };
            players[1] = new string[] { "Hocky", "Rugby", "Badminton" };
            players[2] = new string[] { "Cricket", "Hocky", "Rugby", "Badminton", "Football" };
 
            //Traversing the array
            for (int i = 0; i < players.Length; i++)
            {
                Console.Write("Player {0} can play following games\n--------------------------------------\n", i + 1);
 
                string[] games = players[i];
                for (int j = 0; j < games.Length; j++)
                {
                    Console.Write(games[j]);
                    Console.Write("\n");
                }
                Console.Write("\n\n");
            }
 
            Console.ReadKey();
 
        }
    }
}
 

Result

Output

Player 1 can play following games
--------------------------------------
Cricket
Hocky
Rugby
Badminton


Player 2 can play following games
--------------------------------------
Hocky
Rugby
Badminton


Player 3 can play following games
--------------------------------------
Cricket
Hocky
Rugby
Badminton
Football

Let's learn about the C# Array Class in the next section.