Categories: C#

Jagged Arrays

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." The following examples show how to declare, initialize, and access 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:

C#

int[][] jaggedArray = new int[3][];

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

C#

jaggedArray[0] = new int[5];

jaggedArray[1] = new int[4];

jaggedArray[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 do not need the array size. For example:

C#

jaggedArray[0] = new int[] { 1, 3, 5, 7, 9 };

jaggedArray[1] = new int[] { 0, 2, 4, 6 };

jaggedArray[2] = new int[] { 11, 22 };

You can also initialize the array upon declaration like this:

C#

int[][] jaggedArray2 = new int[][]

{

new int[] {1,3,5,7,9},

new int[] {0,2,4,6},

new int[] {11,22}

};

You can use the following shorthand form. Notice that you cannot omit the new operator from the elements initialization because there is no default initialization for the elements:

C#

int[][] jaggedArray3 =

{

new int[] {1,3,5,7,9},

new int[] {0,2,4,6},

new int[] {11,22}

};

A jagged array is an array of arrays, and therefore its elements are reference types and are initialized to null.

You can access individual array elements like these examples:

C#

// Assign 77 to the second element ([1]) of the first array ([0]):

jaggedArray3[0][1] = 77;

// Assign 88 to the second element ([1]) of the third array ([2]):

jaggedArray3[2][1] = 88;

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:

C#

int[][,] jaggedArray4 = 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 as shown in this example, which displays the value of the element [1,0] of the first array (value 5):

C#

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

The method Length returns the number of arrays contained in the jagged array. For example, assuming you have declared the previous array, this line:

C#

System.Console.WriteLine(jaggedArray4.Length);

will return a value of 3.

Kaynak: msdn2.microsoft.com
belgesi-1692

Belgeci

Recent Posts

Haberi Yazdim, PKK’ya operasyonu engelledim

ABD'nin Kuzey Irak'taki PKK liderlerine kar gizli operasyon planladn yazan gazeteci Robert Novak, bu bilginin…

4 saat ago

Yumurtalı kabak kavurması

Malzeme listesi: -1 kg.kabak -2 adet orta boy soğan -3 tane taze soğan -1 demet…

16 saat ago

Restore Selections for Exchange Mailboxes backed up utilizing Backup Exec 11d 7170 Granular Restore Technology (GRT) may not display all items

When an Exchange Information Store (IS) backup is performed with Backup Exec 11.0 utilizing the…

1 gün ago

How to Backup individual Exchange mailboxes and Public Folders in Backup Exec 11d for Windows Servers

In previous versions of Backup Exec, individual Exchange mailboxes were backed up separately from the…

2 gün ago

Les Essais De Montaigne (fransizca)

                      Les Essais de Montaigne Sur la connaisance de soi [Contre ces gens "qui n'ignorent…

2 gün ago

Michel de Montaigne (fransizca)

                               Michel de Montaigne «Chez aucun autre penseur de l'époque, les différents caractères de la…

3 gün ago