Arrays can be one-dimensional or multi-dimensional. "Rectangle" array and "not neat" array C # are supported.
One-dimensional arrays are the most common types. Example: 1 using System;
2 class Test
3 {
4 static void Main (){
5 int [] arr = new int [5];
6 for (int I = 0; I <arr. Length; I ++)
7 arr [I] = I * I;
8 for (int I = 0; I <arr. Length; I ++)
9 Console. WriteLine ("arr [{0}] = {1}", I, arr [I]);
10}
11}
An int-type one-dimensional array is created. After the elements of the array are initialized, the values of each element are printed. The output is:
Arr [0] = 0
Arr [1] = 1
Arr [2] = 4
Arr [3] = 9
Arr [4] = 16
In this example, the Type int [] is an array type. The array type is represented by a non-array type followed by one or more rank symbols. Example: 1 class Test
2 {
3 static void Main (){
4 int [] a1; // single-dimen1_array of int
5 int [,] a2; // 2-dimen=array of int
6 int [,] a3; // 3-dimen=array of int
7 int [] [] j2; // "jarged" array: array of (array of int)
8 int [] [] [] j3; // array of (array of int ))
9}
10}
Demonstrate the declaration of local variables for various int-type arrays. The array type is the reference type, so the declaration of the array variable only opens up the memory space for the reference to the array. An array instance is actually created by an array constructor or an array constructor.
Example: 1 class Test
2 {
3 static void Main (){
4 int [] a1 = new int [] {1, 2, 3 };
5 int [,] a2 = new int [,] {1, 2, 3}, {4, 5, 6 }};
6 int [,] a3 = new int [10, 20, 30];
7 int [] [] j2 = new int [3] [];
8 j2 [0] = new int [] {1, 2, 3 };
9 j2 [1] = new int [] {1, 2, 3, 4, 5, 6 };
10 j2 [2] = new int [] {1, 2, 3, 4, 5, 6, 7, 8, 9 };
11}
12}
Displays the expressions constructed by various arrays. The a1, a2, and a3 variables are rectangular arrays, while j2 is not neat arrays. "Rectangular array" and "untidy array" are based on the array shape. A rectangle array always has a matrix shape.
Given the length of each dimension of the array, its matrix looks obvious. For example, the dimensions of a3 are 10, 20, and 30 respectively. It is easy to point out that this array contains 10*20*30 elements.
On the contrary, the j2 variable is an untidy array, or an "array ". Specifically, j2 represents an array of the int type, or j2 is a one-dimensional array with an array element of the int [] type. Each int [] type element in it can be instantiated separately, which forms the irregular shape of j2.
In this example, each int [] has a different length. In particular, the j2 [0] length is 3, j2 [1] is 6, j2 [2] is 9.
(Note: In C ++, the int x [3] [5] [7] array is defined to represent a 3-dimensional rectangle array. in C, int [] [] [] defines an untidy array)
The element type and array shape (including the untidy array and rectangular array), as well as the dimension, are part of the array type. In other words, the size of the array, which is determined by the size of each dimension, is not part of the array type.
This distinction is quite clear in the syntax of the language, because the size of each dimension of the array is pointed out in the Array Construction expression, rather than in the array type. For example
Int [,] a3 = new int [10, 20, 30];
Contains an array type int [,] expression, and an Array Construction expression new int [10, 20, 30].
For the declaration of local variables and fields, you can use the abbreviated method as follows:
Int [] a1 = new int [] {1, 2, 3 };
Can be abbreviated
Int [] a1 = {1, 2, 3 };
The semantics remains unchanged.
The context determines the type of the array being initialized. The information of this type is required by the array initiator such as {1, 2, 3.
Example: 1 class Test
2 {
3 static void Main (){
4 short [] a = {1, 2, 3 };
5 int [] B = {1, 2, 3 };
6 long [] c = {1, 2, 3 };
7}
8}
It shows that the same Array initiator can be used on different array types. Because the context is required to determine the type of the array initiator, the array initializer cannot be used unless the array type is explicitly specified.