For convenience, the following arrays are defined as integral types
One , array declaration
C # When declaring an array, square brackets [] must follow the type, not after the identifier
1. Declaring a one-dimensional array
The size of the C # array is not part of its type, but is part of the array type in the C language
int New int [4]; // declare the array type and use the New keyword to allocate memory space for him /* The above statement can be decomposed into the following two steps */ int [] Mysigarray; // declaring an array type New int [4]; // Use the New keyword to allocate memory space for him
In addition, C # declares an array when the square brackets that support allocating memory are integer variables or variable expressions, as long as the value of the variable is determined beforehand (this is not allowed before the C C99 standard appears)
int New int [n]; // the value of n must be determined, and the array size will not change even if the value of n is changed after the declaration
2. Declaring a multidimensional array
int New int [4,3// Declare an array of two-dimensional integers
3. Declaring a jagged array (an array of arrays)
The size of a multidimensional array is rectangular, such as a 3x3 (or 4x3, just the same size as each row) element. And the size of the jagged array is more flexible, in the jagged array, each row can have a different size, such as the number of jagged
int new int[3][]; // Note that unlike a two-dimensional array, there are two square brackets
Ii. Initialization of arrays
If you have initialization data, you can initialize it in the simplest form below.
int[] Mysigarray = {1,2,3,4};//initialization of one-dimensional arraysint[,] Mymularray = {{1,2,3},{4,5,6},{7,8,9}};//initialization of two-dimensional arraysint[] Mysawarray = {New int[] {1,2},New int[] {3,4,5,6,7,8},New int[] {9,Ten, One}};//Jagged Array Initialization
Iii. Access to arrays
1. Accessing a single element in an array initialized above
int i = mysigarray[2]; // i=3 int j = mymularray[2,1]; // j=8 int k = mysawarray[2[1]; // k=10 Note the difference between a two-dimensional array and a jagged array
2. Iterating through the array elements
C # provides a foreach statement that provides a simple, straightforward way to iterate through an array
foreach (int in MyArray) // Add the values of all array elements 1,myarray can be one-dimensional, multidimensional, or jagged arrays { m+ +; }
Alternatively, you can use the For loop to iterate through the array, which allows you to better control the array elements (knowing which array objects are in each loop), which is not possible with the foreach statement.
for (int0; i< mysawarray.length; i++) { for (int0; j < Mysawarray[i]. Length; J + +) { mymularray[i,j]=mysawarray[i][j]; // assigns each element of a jagged array to the corresponding element in a two-dimensional array }}
Iv. Common Properties and methods of arrays
In C #, an array is actually an object. System.Array is an abstract base type for all array types, providing methods for creating, manipulating, searching, and sorting arrays, so that all arrays can use its properties and methods
1. Common Properties
Length: Gets a 32-bit integer that represents the total number of elements in all the dimensions of the array, that is, the number of elements in the array, commonly used to limit the size of the array subscript
2. Common methods
2.1 Copying of arrays
In C #, the replication of an array is generally divided into the following three types:
int[] Pins = {3,9,7,2};int[] Copy1 =New int[Pins. Length]; pins. CopyTo (copy1, 0); //using the CopyTo () method of the Array object, the parameter 0 means that the value copied from the pins array is placed starting from the first element of the Copy1 array (subscript 0) .int[] Copy2 =New int[Pins. Length]; array.copy (pins,copy2,copy2. Length); //use a static method of the array class copy (), parameter copy2. Length indicates the last element (COPY2) from the beginning of the first element of the pins array to the COPY2 array to accommodate. Length) are copied to the Copy2 arrayint[] Copy3 =(int[]) pins. Clone (); //using the Clone () method of the Array object, it creates a new array, the return value is an object, so you need to force the type to be converted to the appropriate type
2.2 Sorting of arrays
Sorting is one of the commonly used algorithms in programming. Two ways to sort arrays are provided in C #:
Array.Sort (array) for small to large sorting of arrays
array.reverse (Array) for reverse sorting of arrays
int [] pins = {3,9,7,2}; Array.Sort (Pins); // pins={2,3,7,9} Array.reverse (pins); // pins={9,7,3,2}
Arrays in C #