Differences between Jagged Array and Multidimensional Array in c#?

Asked

Viewed 864 times

13

There is difference in these arrays in c#?

int[][][]

int[,,]

As far as I can see, it’s the same thing, but c# can’t cast from one to the other.

2 answers

9


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

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

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

It’s even possible to mix the two:

int[][,,][,]

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

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

8

To Jagged Array is a Matrix of Matrices, a int[][] is a matrix 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:

inserir a descrição da imagem aqui

Already the Multidimensional Array (int[,]) is a single memory block (matrix itself). More cohesive looking like a box, square, etc where there are no irregular columns.

Example:

Three-row two-dimensional array across three columns.

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

Illustration.

inserir a descrição da imagem aqui

Reply adapatada of: SOURCE

Browser other questions tagged

You are not signed in. Login or sign up in order to post.