C # Sharp experience 11th lecture Array

Source: Internet
Author: User

11th lecture Array

Array
??????? Array is a common data structure in programming. Like C/C ++, array indexes in C # Start from 0 and their element types must be the same. Of course, implicit transformation due to polymorphism is another theory. Arrays in C # can be divided into one-dimensional arrays, multi-dimensional arrays, and ragged arrays. Their declaration, initialization, and element indexes all have different syntaxes. A major difference between C # And arrays in C ++ is that it ensures its type security by the. NET Universal Language Runtime (CLR), and can dynamically create and expand capacity during runtime.
??????? The declaration and initialization of one-dimensional arrays have specific syntaxes. We usually put them together and read the following code and comments:
// Declare initialization at the same time
Int [] myintarr1 = new int [] {1, 2, 3}; // declare and initialize the 1*3 Array
Int [] myintarr2 = int {1, 2, 3}; // initialization in simplified form

// Separate declaration and initialization
Int [] myintarr3;
Myintarr3 = new int [] {1, 2, 3}; // initialization cannot be simplified.

// You can specify the array capacity during initialization.
Int [] myintarr4 = new int [3] {1, 2, 3 };
// It cannot be int [3] myintarr4 = new int [3] {, 3}
??????? Note that the declaration section [] follows the array element type (such as INT), followed by the variable name. C # does not support the declaration of int A [] = {1, 2, 3, 4, 5} in the traditional C ++. The right side of the equal sign is the initialization part. We can see that C # supports two types of initialization expressions. The latter is the simplification of the former. However, when the Declaration and initialization are separated, we can no longer use a simplified form of initialization. C # The initialization part of the array can specify the size of the array in the brackets [] (the size must be an integer constant or constant. Of course, it must be consistent with the number of array elements initialized later ), the array declaration part cannot contain the array capacity.
??????? C # supports multi-dimensional arrays. Its initialization and C ++ both use nested braces for expression, but its declaration is different from C ++. See the following code and comments:
// Declare initialization at the same time
Int [,] mymularr1 = new int [,] {1, 2, 3}, {2, 4, 6}; // declare and initialize the 2*3 Array
Int [,] mymularr2 = {1, 2, 3}, {2, 4, 6}; // initialization in simplified form

// Separate declaration and initialization
Int [,] mymularr3;
Mymularr3 = new int [,] {1, 2, 3}, {2, 6 }};

// You can specify the array capacity during initialization.
Int [,] mymularr4 = new int [2, 3] {1, 2, 3}, {2, 4, 6 }};
// It cannot be int [2, 3] mymularr5 = new int [2, 3] {1, 2}, {2, 4 }};
??????? C # Use commas (,) in brackets to declare multi-dimensional arrays. Multiple parentheses (such as int [] []) in C #, it is used to represent the following parameter array. The declaration and initialization of multi-dimensional arrays are similar to those of one-dimensional arrays. We can see from the above example.
??????? An array of arrays is similar to the multi-dimensional array we mentioned above, but it is essentially a one-dimensional array, however, its element is an array (it can be a one-dimensional array, a multi-dimensional array, or an array with different parameters ). A notable feature of the parameter array is that the order of the array as its element can be different, which is very visually "uneven. Because it is still a one-dimensional array, the declaration of the parameter array is the same as that of the initialization and one-dimensional array, it is because we always assume it as a multi-dimensional array on the "senses", which makes it easy for us to make mistakes here. Let's look at the following code and comments:
// Declare initialization at the same time
Int [] [] myragarr1 = new int [] [] {New int [] {1, 2, 3}, new int [] {2, 4, 6 }}; // declare and initialize a two-dimensional array whose elements are three-dimensional Arrays
Int [] [] myragarr2 = {New int [] {1, 2, 3}, new int [] {2, 4, 6}; // initialization in simplified form
// The multi-dimensional array initialization statement cannot be used: int [] [] myragarr2 = {1, 2, 3}, {2, 4, 6 }};

// Separate declaration and initialization
Int [] [] myragarr3;
Myragarr3 = new int [2] []; // initializes a one-dimensional array,
// It cannot be like this: myragarr3 = new int [2] [3];
Myragarr3 [0] = new int [] {1, 2, 3}; // initialize the array element
Myragarr3 [1] = new int [] {2, 4, 6}; // initialize the array element

// You can specify the array capacity during initialization.
Int [] [] myragarr4 = new int [2] [] {New int [] {1, 2, 3}, new int [] {2, 4, 6 }};
// Int [2] [] myragarr = new int [2] [] {New int [] {1, 2, 3}, new int [] {2, 4, 6 }};
// It cannot be int [] [] myragarr4 = new int [2] [3] {New int [] {1, 2, 3}, new int [] {2, 4, 6 }};

// A typical "parameter" Array
Int [] [] myragarr5 = new int [3] [];
Myragarr5 [0] = new int [] {1, 2, 3, 4, 5 };
Myragarr5 [1] = new int [] {1, 2, 3 };
Myragarr5 [2] = new int [] {1, 2, 3, 4, 5, 6, 7, 8 };
??????? C # use multiple brackets to represent the array of the difference (array ). The above code and comments show in detail how to declare and initialize the parameter array. Most of the behavior is the same as the previous experience from a one-dimensional array, but there is only one point to note: Why can we only specify constants or constants in the first square brackets when specifying the size of an array, but cannot specify the capacity in the brackets below it? This can be found from the essential one-dimensional array of the parameter array. We say that when initializing the parameter array, we actually initialize each element in the one-dimensional array. According to the rules of the one-dimensional array, we can only specify the number of these elements, that is, the capacity of the one-dimensional array, that is, the constant or constant in the first brackets of the parameter array.
??????? The array in C # is essentially a managed system. array type. Of course, it also has all the interface operations of system. array. It is worth noting that system. array is an abstract type. We cannot directly declare system. array to obtain the array type -- in fact, system. array should not be considered as an array type at all. It only provides interface operations for us through system services. Many Attributes and methods of system. array are very useful for operating arrays. This article will not list them one by one. The following is just a complete demonstration example:
Using system;
Class Test
{
???? Public static void main ()
???? {
???????? // One-dimensional array
???????? Int [] myintarr = new int [] {1, 2, 3, 4, 5 };
???????? Foreach (int I in myintarr)
???????????? Console. Write (I );
???????? Console. writeline ("/nthe length:" + myintarr. Length + "/N ");
????????
???????? // Multi-dimensional array
???????? Int [,] mymularr = new int [,] {1, 2, 3}, {2, 4 }};
???????? Foreach (int I in mymularr)
???????????? Console. Write (I );

???????? Console. writeline ();
????????
???????? For (INT I = 0; I ???????? {
???????????? Console. writeline ();
???????????? For (Int J = 0; j ???????????????? Console. Write (mymularr [I, j]);
????????}
???????? Console. writeline ("/nthe length:" + mymularr. Length + "/N ");
????

???????? // The parameter Array
???????? Int [] [] myragarr = new int [3] [];
???????? Myragarr [0] = new int [] {1, 2, 3, 4, 5 };
???????? Myragarr [1] = new int [] {1, 2, 3 };
???????? Myragarr [2] = new int [] {1, 2, 3, 4, 5, 6, 7, 8 };
???????? For (INT I = 0; I ???????? {
???????????? Console. writeline ();
???????????? For (Int J = 0; j ???????????????? Console. Write (myragarr [I] [J]);
????????}
???????? Console. writeline ("/nthe length:" + myragarr. Length );
????}
}

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.