Differences between Jagged Array and Multidimensional Array in C #?

12

Is there a difference in these arrays in C #?

int[][][]

int[,,]

What I see is the same thing, but C # can not cast from one to another.

    
asked by anonymous 07.08.2016 / 18:52

2 answers

8

Cast is not possible because int[][][] and int[,,,] declare different arrays .

int[][][] is a array of arrays of arrays ( Jagged array ). Each element of the first array is an array which in turn has an array element. Each of the array can have different dimensions.

int[,,,] is a array with three dimensions ( Multidimensional Array ), each element is a int . Each row will always have the same number of columns.

You can even mix both:

int[][,,][,]

The preceding code declares a array one-dimensional ( Single-Dimensional Array ) of two-dimensional arrays of type int .

The notation [] indicates that the object is an array , the commas indicate the number of array dimensions.

    
07.08.2016 / 19:00
7

The Jagged Array is a Matrix Array, a int[][] is an array of int[] . Elements can contain different sizes and dimensions.

Example:

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

Illustration of the above code:

The Multidimensional Array (int[,]) is a single block of memory (array itself ). More cohesive looking like a box, square, etc. in which there are no irregular columns.

Example:

Two-dimensional array of three rows by three columns.

int [,] multidimensional  = new int[3, 3] {{1,2,3}, {4,5,6}, {7,8,9}};

Illustration.

Adaptedanswerfrom: SOURCE

    
30.11.2016 / 12:00