神刀安全网

Jagged Arrays in C#

In this article, We will discuss:

  • What are Jagged Arrays in C#?
  • Declare a Jagged Array
  • Instantiating a Jagged Array
  • SubArrays in Jagged Array
  • Difference between Rectangular and Jagged Array
  • Summary

What are Jagged Arrays in C#?

A jagged array is an array of arrays. Unlike rectangular arrays, the subarrays of a jagged array can have different numbers of elements.

Let’s look at below example to understand it better.

The code below declares a two-dimensional jagged array.

int[][] jagArr = new int[3][]; // Declare and create top-level array. ... // Declare and create subarrays. 

The length of the first dimension is 3.The declaration can be read as “jagArr is an array of three arrays of ints.”

A jagged array can be of any number of dimensions greater than one.In rectangular arrays, dimension lengths cannot be included in the array type section of the declaration.

Syntax for jagged arrays requires a separate set of square brackets for each dimension. The number of sets of square brackets in the declaration of the array variable determines the rank of the array.

Instantiating a Jagged Array:

jagged array is an array of independent arrays, each array must be created separately. Instantiating a full jagged array requires the following steps:

1. Instantiate the top-level array.

2. Instantiate each subarray separately, assigning the reference of the newly created array to the appropriate element of its containing array.

Let’s look at below example to understand it better.

int[][] Arr = new int[3][]; // 1. Instantiate top level. Arr[0] = new int[] {10, 20, 30}; // 2. Instantiate subarray. Arr[1] = new int[] {40, 50, 60, 70}; // 3. Instantiate subarray. Arr[2] = new int[] {80, 90, 100, 110, 120}; // 4. Instantiate subarray. 

Jagged Arrays in C#

Subarrays in Jagged Arrays:

Subarrays in a jagged array are themselves arrays, it’s possible to have rectangular arrays inside jagged arrays.

class Program     {         static void Main()         {             int[][,] Arr; // An array of 2D arrays             Arr = new int[3][,]; // Instantiate an array of three 2D arrays.             Arr[0] = new int[,] { { 10, 20 },{ 100, 200 } };             Arr[1] = new int[,] { { 30, 40, 50 },{ 300, 400, 500 } };             Arr[2] = new int[,] { { 60, 70, 80, 90 },{ 600, 700, 800, 900 } };//Get length of dimension 0 of Arr.             for (int i = 0; i < Arr.GetLength(0); i++)             {//Get length of dimension 0 of Arr[ i ].                 for (int j = 0; j < Arr[i].GetLength(0); j++)                 {//Get length of dimension 1 of Arr[ i ].                     for (int k = 0; k < Arr[i].GetLength(1); k++)                     {                         Console.WriteLine                         ("[{0}][{1},{2}] = {3}", i, j, k, Arr[i][j, k]);                     }                     Console.WriteLine("");                 }                 Console.WriteLine("");             }         }     } 

Output:

[0][1,0] = 100 [0][1,1] = 200   [1][0,0] = 30 [1][0,1] = 40 [1][0,2] = 50   [1][1,0] = 300 [1][1,1] = 400 [1][1,2] = 500   [2][0,0] = 60 [2][0,1] = 70 [2][0,2] = 80 [2][0,3] = 90   [2][1,0] = 600 [2][1,1] = 700 [2][1,2] = 800 [2][1,3] = 900 

Difference between Jagged Array and Rectangular Array:

The structure of rectangular and jagged arrays is significantly different.One-dimensional arrays have specific instructions in the CIL that allow them to be optimized for performance. Rectangular arrays do not have these instructions and are not optimized to the same level.

It is more efficient to use jagged arrays of one-dimensional arrays—which can be optimized—than rectangular arrays.On the other hand, the programming complexity can be significantly less for a rectangular array because it can be treated as a single unit, rather than an array of arrays.

Jagged Arrays in C#

In above diagram, the structure of a rectangular three-by-three array, as well as a jagged array of three one-dimensional arrays of length 3.

-Both arrays hold nine integers, but as you can see, their structures are quite different.

-The rectangular array has a single array object, while the jagged array has four array objects.

Summary:

In this article, We have discussed:

  • What are Jagged Arrays in C#?
  • Declare a Jagged Array
  • Instantiating a Jagged Array
  • SubArrays in Jagged Array
  • Difference between Rectangular and Jagged Array

Thanks for visiting !!

© 2016,admin. All rights reserved.

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » Jagged Arrays in C#

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址