Export (0) Print
Expand All

Jagged Arrays

A jagged array is an array whose elements are arrays. The elements of a jagged array can be of different dimensions and sizes. A jagged array is sometimes called an "array-of-arrays." This topic contains examples of declaring, initializing, and accessing jagged arrays.

The following is a declaration of a single-dimensional array that has three elements, each of which is a single-dimensional array of integers:

int[][] myJaggedArray = new int[3][];

Before you can use myJaggedArray, its elements must be initialized. You can initialize the elements like this example:

myJaggedArray[0] = new int[5];
myJaggedArray[1] = new int[4];
myJaggedArray[2] = new int[2];

Each of the elements is a single-dimensional array of integers. The first element is an array of 5 integers, the second is an array of 4 integers, and the third is an array of 2 integers.

It is also possible to use initializers to fill the array elements with values, in which case you don't need the array size, for example:

myJaggedArray[0] = new int[] {1,3,5,7,9};
myJaggedArray[1] = new int[] {0,2,4,6};
myJaggedArray[2] = new int[] {11,22};

You can also initialize the array upon declaration like this:

int[][] myJaggedArray = new int [][] 
                        {
                           new int[] {1,3,5,7,9},
                           new int[] {0,2,4,6},
                           new int[] {11,22}
                        };

You can use the following shortcut (notice that you cannot omit the new operator from the elements initialization because there is no default initialization for the elements):

int[][] myJaggedArray = {
                           new int[] {1,3,5,7,9},
                           new int[] {0,2,4,6},
                           new int[] {11,22}
                        };

You can access individual array elements like these examples:

// Assign 33 to the second element of the first array:
myJaggedArray[0][1] = 33;
// Assign 44 to the second element of the third array:
myJaggedArray[2][1] = 44;

It is possible to mix jagged and multidimensional arrays. The following is a declaration and initialization of a single-dimensional jagged array that contains two-dimensional array elements of different sizes:

int[][,] myJaggedArray = new int [3][,] 
                         {
                            new int[,] { {1,3}, {5,7} },
                            new int[,] { {0,2}, {4,6}, {8,10} },
                            new int[,] { {11,22}, {99,88}, {0,9} } 
                         };

You can access individual elements like this example, which displays the value of the element [1,0] of the first array (value 5):

Console.Write("{0}", myJaggedArray[0][1,0]);

Example

This example builds an array, myArray, whose elements are arrays. Each one of the array elements has a different size.

// cs_array_of_arrays.cs
using System;
public class ArrayTest 
{
   public static void Main() 
   {
      // Declare the array of two elements:
      int[][] myArray = new int[2][];

      // Initialize the elements:
      myArray[0] = new int[5] {1,3,5,7,9};
      myArray[1] = new int[4] {2,4,6,8};

      // Display the array elements:
      for (int i=0; i < myArray.Length; i++) 
      {
         Console.Write("Element({0}): ", i);

         for (int j = 0 ; j < myArray[i].Length ; j++)
            Console.Write("{0}{1}", myArray[i][j],
                          j == (myArray[i].Length-1) ? "" : " ");

         Console.WriteLine();
      }
   }
}

Output

Element(0): 1 3 5 7 9
Element(1): 2 4 6 8

See Also

Arrays | Single-Dimensional Arrays | Multidimensional Arrays

Show:
© 2014 Microsoft