There are multidimensional and jagged arrays in C #, what's the difference between them?
It is straightforward that each row of a multidimensional array is fixed, and each line of a jagged array can have a different size.
With two-dimensional examples, two-dimensional arrays are the matrix of MXN, m rows n columns, and jagged arrays (also called jagged arrays) have m rows, but each row is not necessarily n columns. Got it?
Also note that the array in C # is also a type (not in C + +)!
Here's a look at the example:
Two-dimensional arrays:
Public Static void Main(){introw =5;intColumn =5;int[,] matrix =New int[Row, column];//Initialize for(inti =0; i < row; i++) { for(intj =0; J < column; J + +) {matrix[i, j] = (i +1) *Ten+ j +1; } }//OutputConsole.WriteLine ("The two-dimensional array has: {0} row {1} column!" ", Matrix. GetLength (1), Matrix. GetLength (1)); for(inti =0; i < row; i++) { for(intj =0; J < column; J + +) {Console.Write (Matrix[i, J] +" "); } console.write (' \ n '); }}
Results:
Description
The declaration of a multidimensional array takes int[,] this way
Gets the length of the dimension of the multidimensional array with the array name. GetLength (i) method
For example, get the row for a two-dimensional array: matrix. GetLength (0); Gets the columns of the two-dimensional array: matrix. GetLength (1)
The length property of a multidimensional array is the total size of the array
Two-dimensional jagged arrays:
public static void Main () {introw =5;//interleaving array space request int[][]Matrix= newint[Row] []; for(inti =0; i < row; i++) {Matrix[I] = newint[i +1]; }//Data initialization for(inti =0; I <Matrix. Length; i++) { for(intj =0; J <Matrix[i]. Length; J + +) {MatrixI [j] = (i +1) *Ten+ j +1; } }//Data Output for(inti =0; I <Matrix. Length; i++) { for(intj =0; J <Matrix[i]. Length; J + +) {Console.Write (MatrixI [j] +" "); } console.write (' \ n '); }}
Results:
Description
The declaration of a jagged array uses the form of int[][]
To get the length of a jagged array directly using the Length property
Multidimensional and jagged arrays in C #