C # sharp experience in the eleventh

xiaoxiao2021-03-06  68

Eleventh speaking array

Array ??????? Array is a data structure that is commonly used in programming. Like C / C , the array index in the C # starts from 0, and its element type must be the same, of course, the implicit transformation of the polymorphism is another matter. The array in C # can be divided into one-dimensional array, multi-dimensional array, and Ragged Array, their respective declarations, initialization, and element indexes have different grammatical provisions. C # A significant difference in the array in C is that it is running when it is running (CLR) by .NET Universal Language (CLR) guarantees its type security, you can create and expand capacity during runtime. ??????? One-dimensional array declaration and initialization have a specific syntax, we usually put them together, look at the following code and notes: // Statement initializes int [] MyinTarr1 = new int = new int [] {1 , 2, 3}; // Declaration and initialize 1 * 3 array int [] MyinTarr2 = int {1, 2, 3}; // Simplified form of initialization // declaration and initialization Int, Int [] MyinTarr3; MyinTarr3 = new int int INT [] {1, 2, 3}; // Cannot specify an array capacity int [] {1, 2, 3}; // cannot be specified in the initialization // initialization of simplified forms. Int [3] MyinTarr4 = new int [3] {1, 2, 3} ??????? Note that the brackets here [] are followed by the array element type (such as int), and then the variable name. C # does not support the INT A [] = {1, 2, 3, 4, 5} declaration of traditional C . The right side is the initialization section, we can see that C # supports two initial expression forms, which is the simplification of the former. However, in the case of declaration and initialization, we cannot use the initialization of simplified forms. The initialization portion of the C # array can specify the capacity of the array in the middle bracket [] (which must be an integer constant or constant, of course, the number of array elements that must be inherently, and the array declaration portion cannot contain the capacity of the array. ??????? C # supports multi-dimensional arrays, which use nested braces in the same initialization and C , but its declaration is different from C , see the following code and annotation: // Initialization INT [,] mymularr1 = new int [] {{1, 2, 3}, {2, 4, 6}}; // declaration and initialization 2 * 3 array int [=,] mymularr2 = {{1, 2, 3}, {2, 4, 6}}; // Simplified form of initialization // declaration and initialization INT [,] mymularr3; mymularr3 = new int [,] {{1, 2, 3}, {2, 4 , 6}}; // Initialization can specify the capacity int [2, 3] {1, 2, 3}, {2, 4, 6}}}, {2, 4, 6}}}, {2, 4, 6}}; // cannot INT [2,3] mymularr5 = new int [2, 3] {{1, 2, 3}, {2, 4, 6}}; ??????? c # adopts a comma in brackets "," The method is declared a multi-dimensional array, a plurality of brackets (such as int [] [] a) expressing the way to represent the lower part of the above. The declaration and initialization of multi-dimensional arrays are similar to the one-dimensional array, we can see from the above example. ?????? A array of array, similar to the array of array, similar to the multi-dimensional array mentioned above, but it is still a one-dimensional array, but its elements Is an array (can be a one-dimensional array, a multi-dimensional array, or an array). A significant feature of the atrial array is that the order of the array of elements can be different, and it is very imageable as "the incoming".

Since the essence is still a one-dimensional array, the declaration of the array is the same as the initialization and one-dimensional array of array, but because we are in the "sense", it is easy to make mistakes here, look at the following Code and Note: // Initialization INT [] [] Myragarr1 = new int [] {new int}}; // Declare and initialize the two-dimensional array, its element is three-dimensional array int [] [] myragarr2 = {new int [] {1, 2, 3}, new int} {2, 4, 6}}; // Simplified Form Initialization / / Unlike the initialization statement of multi-dimensional array: int [] [] Myragarr2 = {{1, 2, 3}, {2, 4, 6}}; // Declaration and Initialization INT [] [] Myragarr3; Myragarr3 = new int [2] []; // Initialize one-dimensional array, // can not be like this: myragarr3 = new int [2] [3]; myragarr3 [0] = new int = {1, 2, 3}; // Initialization array element MYRAGARR3 [1] = new int [] {2, 4, 6}; // Initialization array element // Initialization can specify an array capacity int [] [] myragarr4 = new int [2 ] [] {new int [] {1, 2, 3}, new int [] {2, 4, 6}}; // can not int [2] [] myragarr = new int [2] [] {new Int [] {1, 2, 3}, new int}; // can not int [] [] Myragarr4 = new int [2] [3] {new int [] { 1, 2, 3}, new int [] {2, 4, 6}}; // a typical "partial" 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 in] {1, 2, 3, 4, 5, 6, 7, 8}; ??????? C # uses a plurality of middle brackets to express the varying array (array of arrays). The above code and annotations are detailed to show the usage of the atrial array in declaration and initialization. Most behaviors are the same as those from the one-dimensional array, but only one thing needs to point out: Why can we only specify the constant or constant in the first middle bracket when the capacity of the array is specified, not Specify the capacity in brackets in it? This can find the answer from the essential one-dimensional array of the varying array. We said that when we initialize the array array, we are actually in initializing the individual elements in a 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 of the first neutral brackets. ??????? C # Array in nature is a hosted system.Array type, of course it also has all interface operations of System.Array. It is worth pointing out that System.Array is an abstract type, we can't declare system.Array to get an array type - In fact, System.Array should not be considered as an array type, which is just through system services for us Provide interface operations.

Many of the attributes and methods of System.Array are very useful for our operand. This article is no longer listed. Only just give a complete exemplary example to throw the jade. 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 [,] mymular = new int = ,] {{1, 2, 3}, {2, 4, 6}}; ???????? foreach (int I in mymularr) ???????????? console.write (i); ???????? console.writeLine (); ???????????????? for (int i = 0; i <2; i )??? ????? {???????????? console.writeline (); ???????????? for (int J = 0; J <3; J )? ??????????????? console.write (MyMularR [i, j]); ????????} ???????? console.writeLine ("/ NTHE Length: " mymularr.length " / n "); ????????????? // Table 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);

????}

}

转载请注明原文地址:https://www.9cbs.com/read-119996.html

New Post(0)