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
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}; // One-dimensional
Int [,] a2 = new int [,] {1, 2, 3}, {4, 5, 6}; // two-dimensional
Int [,] a3 = new int [10, 20, 30]; // 3D
Int [] [] j2 = new int [3] []; // Variable Length
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, variable length
Arrays are a little different, and each dimension must be defined separately. For example, the first dimension of j2 is 3, the second dimension is 6
The total length is: 1*3 + 1*6 + 1*9 = 18.
Array assignment is a rigorous style. In some cases, we can simplify the writing, but I always think this simplification.
Too many application restrictions and error-prone. I will not introduce it here. Here is another example to illustrate how the parameters in the function are
Assign value */
Class Test
{
Static void F (long [] arr ){}
Static void Main (){
F (new longt [] {1, 2, 3 });
}
}