The establishment and operation of C # multidimensional arrays __c#

Source: Internet
Author: User

1c# How to define and use multidimensional arrays

It is not recommended to use ArrayList, when the elements in the array is a value type in the operation will be a lot of boxing and unboxing of the performance will be a lot of loss, but what type of elements should be used to create what type of array, unless your elements are cross or uncertain to consider using ArrayList.


Multidimensional arrays are defined as follows:
Arrays can have multiple dimensions. For example, the following declaration creates a two-dimensional array of four rows and two columns:
C#

Int[,]array = new int[4, 2];

In addition, the following declaration creates an array of three-dimensional (4, 2, and 3):
C#

Int[,,] array1 = new Int[4, 2, 3];

Array initialization
You can initialize an array when you declare it, as shown in the following example:
C#

int[,]array2d = new int[,] {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
Int[,,] Array3D = new int[,,] {{1, 2, 3}}, {{4, 5, 6}}};
You can also initialize an array without specifying a level:
C#

Int[,]array4 = {1, 2}, {3, 4}, {5, 6}, {7, 8}};
If you choose to declare an array variable without initializing it, you must assign an array to the variable using the new operator. For example:
C#

Int[,]array5;
Array5 = new int[,] {{1, 2}, {3, 4}, {5, 6}, {7, 8}}; Ok
Array5 = {{1,2}, {3,4}, {5,6}, {7,8}}; Error
You can also assign values to an array element, for example:
C#

array5[2,1] = 25;


Write a detailed example that holds the contents of each cell in a table in a dynamic string array:

Datatabledt = ds. tables[0];//take out a table with a dynamic content
string[,] str = NEWSTRING[DT. Rows.count,dt. Columns.count];

Use array str to store the contents of each cell in a table

Dt. Rows.Count is the number of rows in the table, Dt.Columns.Count is the number of tables
for (int i =0;i < dt. Rows.Count; i++)
{
for (int j=0;j<dt. columns.count;j++)
{
STR[I,J] =dt. ROWS[I][J]. ToString (). Trim ();
}
}

2 defining an indefinite length array

One dimension:

int[] numbers = new int[]{1,2,3,4,5,6}; Indefinite length

int[] numbers = new int[3]{1,2,3};//fixed length

Multidimensional

int[,] numbers = newint[,]{{1,2,3},{1,2,3}; Indefinite length

int[,] numbers = new int[2,2]{{1,2},{1,2}};//fixed length

Instance:

A:int[] mf1=newint[6];

Note the scope of the initialization array, or specify an initial value; A one-dimensional array of integers containing 6 elements, with the initial value 1,2,3,4,5,6

Int[]mf2=new int[6]{1,2,3,4,5,6};

b://a one-dimensional string array, and if an initializer is provided, you can also omit the new operator

String[] mf3={the "C", "C + +", "C #"};

c://a one-dimensional array of objects

object[] Mf4 = new Object[5] {26, 27, 28, 29, 30};

d://two-dimensional array of integers, initial value mf5[0,0]=1,mf5[0,1]=2,mf5[1,0]=3,mf5[1,1]=4

Int[,]mf5=new int[,]{{1,2},{3,4}};

E://6*6 Two-dimensional integer array

Int[,]mf6=new mf[6,6];

3 instance definition one and two dimensional arrays


[CSharp] view plaincopy using system;   using system.collections.generic;   using  system.linq;   using system.text;      Namespace consoleapplication1    {       class Program       {            static void main (String[] args)             {                //one-dimensional array definition and initialization                 int[] one1 = new int[] {3,2,1 };//the first way                 int[] one2 = { 3, 2 ,  1 };      //the second way                 int[] one3;                     //Third Way                 one3=new int[]{3,2,1};                                 //two-dimensional array definition and initialization                    //Irregular two-dimensional arrays                int[] [] array = new int[2][];                array[0] = new int[3];                array[0][1] = 11;                array[0][2] = 12;                array[1] = new int[] { 1, 2, 3, 4,5 };                   //or int[][]  array = new int[2][] { new int[] {0,11,12 }, new int[]  {1,2,3,4,5 }};                            console.writeline (  "Irregular two-dimensional array:    ");               for  ( Int i = 0; i < array. length; i++)                {    &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;    foreach  (Int j in array[i])                     {                        console.write ( j+ "   ");  

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.