Multidimensional and jagged arrays in C #

Source: Internet
Author: User

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 #

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.