Study on c # (5) Array type (Array types)

Source: Internet
Author: User

Please do not rush! I have been busy recently. I did not find any information on hand. If a friend wants to read Chinese, he just gave me some help. But he does not like bbs. (I am also reading and translating, it must be a hundred mistakes. So I added some personal opinions and supplemented some stuff as a remedy. At the beginning, I did not expect anyone to be interested or intend to write it completely. I hope you will discuss more about the vulnerabilities. Improvement. Thank you!

1.5 Array type (Array types)

Arrays can be one-dimensional or multi-dimensional. The members of the ancestor can be neat or long.

One-dimensional arrays are the most common and simple. Here is an example, so we will not explain it much.
*/
Using System;
Class Test
{
Static void Main (){
Int [] arr = new int [5];
For (int I = 0; I <arr. Length; I ++)
Arr [I] = I * I;
For (int I = 0; I <arr. Length; I ++)
Console. WriteLine ("arr [{0}] = {1}", I, arr [I]);
}
}

/* The result is as follows:
Arr [0] = 0
Arr [1] = 1
Arr [2] = 4
Arr [3] = 9
Arr [4] = 16

Please do not rush! I have been busy recently. I did not find any information on hand. If a friend wants to read Chinese, he just gave me some help. But he does not like bbs. (I am also reading and translating, it must be a hundred mistakes. So I added some personal opinions and supplemented some stuff as a remedy. At the beginning, I did not expect anyone to be interested or intend to write it completely. I hope you will discuss more about the vulnerabilities. Improvement. Thank you!

1.5 Array type (Array types)

We can also compare the definitions and values of multi-dimensional, rule, and variable-length arrays :*/
Class Test
{
Static void Main (){
Int [] a1 = new int [] {1, 2, 3 };File: // 1Dimension
Int [,] a2 = new int [,] {1, 2, 3}, {4, 5, 6 }};File: // 2Dimension
Int [,] a3 = new int [10, 20, 30];File: // 3Dimension
Int [] [] j2 = new int [3] [];File: // changeLong
J2 [0] = new int [] {1, 2, 3 };
J2 [1] = new int [] {1, 2, 3, 4, 5, 6 };
J2 [2] = new int [] {1, 2, 3, 4, 5, 6, 7, 8, 9 };
}
}
/*
The above example shows arrays of various styles. The a1, a2, and a3 variables are rule arrays. J2 is a variable-length array.
The rule array can easily calculate their length. For example, the a3 length is 10*20*30 = 6000. On the contrary, the variable-length array is a bit different, and each dimension of it must be defined separately. For example, the first dimension of j2 is 3, the second is 6, and the third is 9, so the total length is: 1*3 + 1*6 + 1*9 = 18.

The assignment of arrays is a rigorous style. In some cases, we can simplify the writing, but I always think that there are too many restrictions on simplifying the application and it is prone to errors. I will not introduce it here. Here is another example to illustrate how to assign values to parameters in a function */
Class Test
{
Static void F (long [] arr ){}
Static void Main (){
F (new longt [] {1, 2, 3 });
}
}

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.