C # language series lecture (12) array

xiaoxiao2021-03-06  79

One-dimensional and multi-dimensional array arrays are commonly used in programming, like C / C , and the array index in C # starts from 0, and its element type must be the same, of course, the implicit transformation of the polymorphism. Another look. The array in C # can be divided into one-dimensional array, multidimensional array, and Ragged Array, their respective declarations, initialization, and element indexes have different syntax regulations. C # A significant difference between the C array is that it is ensured by the .NET Universal Language Run Environment (CLR) guarantees its type security and can be dynamically created and expanded at runtime. One-dimensional array declarations and initialization have specific syntax, we usually put them together, look at the following code: // Initial intimize int [] MyinTarr1 = new int [] {1, 2, 3}; // declaration And initialize 1 × 3 array int [] MyinTarr2 = int {1, 2, 3}; // Simplified formal declaration and initialization // declaration and initialization INT [] MyinTarr3; MyinTarr3 = new int [] {1, 2, 3}; // Do not use the initialization // initialization of simplified forms to specify the capacity INT [] MyinTarr4 = new int [3] {1, 2, 3}; // No: int [3] MyinTarr4 = New int [3] {1, 2, 3} Note that the brackets of the declaration part ([]) are followed by the array element type (such as int), and then the variable name. C # does not support traditional C "INT A [] = {1, 2, 3, 4, 5}" declaration mode. The right side is the beginning of the initialization, we can see that C # supports two initial expression forms, which are the simplification of the former, but in the case of declaration and initialization, the initialization of simplified form cannot be used. The initialization portion of the C # array can specify the capacity of the array in parentheses ([]) (which must be an integer constant or constant, of course, the number of array elements that must be and later, and the array declaration portion cannot contain an array capacity. C # supports multi-dimensional arrays, which use nested braces in the same initialization and C , but its declaration and C are different, see the following code and comments: // Statement initialize int [,] mymularr1 = new INT [,] {1, 2, 3}, {2, 4, 6}}; // Declaration and initialize 2 × 3 array int [,] mymularr2 = {{1, 2, 3}, {2, 4 , 6}}; // Simplified forms of initialization // declaration and initialization INT [,] mymularr3; mymularr3 = new int [,] {{1, 2, 3}, {2, 4, 6}}; // Initialization can specify an array of capacity int [,] mymularr4 = new int [2, 3] {{1, 2, 3}, {2, 4, 6}}; // No: int [2,3] MymularR5 = New int [2, 3] {{1, 2, 3}, {2, 4, 6}}; C # in parentheses with comma (,) segmentation methods to declare multi-dimensional array, multiple brackets (such as int [ ] [] a) The expression is used to represent the paragraph of the C #. The declaration and initialization of multi-dimensional arrays are similar to the one-dimensional array, we can see from the above example. ARRAY OF ARRAY, a Array Of Array, which is similar to the array of array, which is similar to the multi-dimensional array mentioned above, but it is still a one-dimensional array, but its elements are an array (can be One-dimensional array, multidimensional array or a variant). A significant feature of the atrial array is that as a group of groups of its elements can be different, and it is very imageable as "anesthesia".

Since the essence is still a one-dimensional array, the declaration of the toxic array is the same as the provisions of the initialization and one-dimensional array, but because of the "feel", it is easy to make mistakes here, see the following code. and NOTE: // declaration also initializes the int [] [] MyRagArr1 = new int [] [] {new int [] {1,2,3}, new int [] {2,4,6}}; // Declare and initialize the two-dimensional array, the element is three-dimensional array int [] [] myragarr2 = {new int [] {1, 2, 3}, new int [] {2, 4, 6}}; // Simplified form Initialization / / Do not use the initialization statement of the 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}}; // No: int [2] [] Myragarr = new int [2] [] {new int int int INT [] {1,2,3}, new int}}; // can not: int [] [] Myragarr4 = new int = inr [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 parentheses and an array of arrays (arrays). The above code and annotations show the usage of the atrial array in declaration and initialization. Most behaviors have the same experience from the one-dimensional array, but only one thing needs to point out: Why can only specify a constant or constant in its first parentheses when specifying the array capacity, instead of specifying its posterior brackets Is the capacity? This can find the answer from the essence of the toast array - one-dimensional array. When initializing the toast array, it is actually in initializing the individual elements in one-dimensional array. According to the provision of the one-dimensional array, only the number of these elements can only be specified, that is, the capacity of the one-dimensional array, that is, the constant or constant in the first parentheses of the partial array. The arrays in the C # are essentially 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.

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

New Post(0)