multidimensional arrays in C #

Source: Internet
Author: User

multidimensional Arrays are also known as rectangular arrays.

  You can declare a two-dimensional array of string variables, as follows:

String [,] names;
Alternatively, you can declare a three-dimensional array of int variables, as follows:
Int [ , ] M ;

Two-dimensional arrays:
The simplest form of a multidimensional array is a two-dimensional array. A two-dimensional array, in essence, is a list of one-dimensional arrays. 
a two-dimensional array can be thought of as a table with x rows and y columns. Here is a two-dimensional array containing 3 rows and 4 columns:
a[0][0] a[0][1] a[0][2] a[0][3]
A[1][0] a[1][1] a[1][2] a[1][3]
A[2][0] a[2][1] a[2][2] a[2][3]
A[3][0] a[3][1] a[3][2] a[3][3]
Therefore, each element in the array is identified using the element name in the form a[I, j], where A is the array name, and I and J are the subscripts that uniquely identify each element in a.


Initialize a two-dimensional array:
multidimensional arrays can be initialized by specifying a value for each row within parentheses. The following is an array with 3 rows and 4 columns.
Int [,]A= New Int [3,4] { {0, 1, 2, 3} , /* Initialize the line with index number 0 */ {4, 5, 6, 7} , /* Initialize the line with index number 1 */ {8, 9, 10, 11} /* Initialize the line with index number 2 */}; 
To access two-dimensional array elements:
Elements in a two-dimensional array are accessed by using subscripts (that is, the row and column indexes of the array). For example:
int= a[2,3];
The above statement gets the 4th element in the 3rd row of the array. You can use the above to verify. Let's take a look at the following program, we'll use nested loops to work with two-dimensional arrays:
Using System;Namespace Arrayapplication{ Class MyArray { Static void Main(String[]Args) { /* An array with 5 rows and 2 columns */ Int[,]A= New Int[5, 2] {{0,0}, {1,2}, {2,4}, {3,6}, {4,8} }; IntI,J; /* The value of each element in the output array */ For (I= 0;I< 5;I++) { For (J= 0;J< 2; J++) { Console. Writeline ( "a[{0},{1}] = {2}" , I , J, A[ i,j); } } console . readkey (); } }} /span> When the above code is compiled and executed, it produces the following results:
A[0,0]: 0A[0,1]: 0A[1,0]: 1A[1,1]: 2A[2,0]: 2A[2,1]:  4a[3,0 ]: 3a[ 3,1]:  6a[4,0 ]: 4a[ 4,1]:  8                  

multidimensional arrays in C #

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.