An array is a data structure, which is declared as follows:
type[] arrayName;
The array has the following properties:
1. Arrays can be one-dimensional, multi-dimensional or interlaced.
2. The default value of the numeric array element is set to zero, while the default value of the reference element is set to null.
3. An interleaved array is an array of arrays, so its elements are reference types and are initialized to null.
4. The index of the array starts from zero: The index of an array with n elements is from 0 to n-1.
5. Array elements can be of any type, including array types.
One-dimensional array
//Declare a one-dimensional array without initialization, equals null int[] intArray1; //Initialize the declared one-dimensional array intArray1 = new int[3]; //The default value of array elements is 0 intArray1 = new int[3]{1,2,3}; intArray1 = new int[]{1,2,3}; //Declare a one-dimensional array and initialize it at the same time int[] intArray2 = new int[3]{1,2,3}; int[] intArray3 = new int[]{4,3,2,1}; int[] intArray4 = {1,2,3,4}; string[] strArray1 = new string[]{"One","Two","Three"}; string[] strArray2 = {"This","is","an","string","Array"};
Multidimensional array
//Declare two-dimensional array without initialization short[,] sArray1; //Initialize the declared two-dimensional array sArray1 = new short[2,2]; sArray1 = new short[2,2]{{1,1},{2,2}}; sArray1 = new short[,]{{1,2,3},{4,5,6}}; //Declare two-dimensional arrays and initialize them at the same time short[,] sArray2 = new short [1,1]{{100}}; short[,] sArray3 = new short [,]{{1,2},{3,4},{5,6}}; short[,] sArray4 = {{1,1,1},{2,2,2}}; //Declare three-dimensional arrays and initialize them at the same time byte[,,] bArray1 = {{{1,2},{3,4}},{{5,6},{7,8}}};
Interleaved array
//Declare interleaved array without initialization int[][] JagIntArray1; //Initialize the declared interleaved array JagIntArray1 = new int [2][] { new int[]{1,2}, new int[]{3,4,5,6} }; JagIntArray1 = new int [][]{ new int[]{1,2}, // new int []{3,4,5}, intArray2 //Use int[] array variable }; //Declare the interleaved array and initialize it at the same time int[][] JagIntArray2 = { new int[]{1,1,1}, //new int []{2,2}, intArray1 };
An array is an element in the data set that is grouped together with a common name and accessed through the assigned subscript.
An array is a set of data of the same type. When accessing data in an array, it can be specified by subscript. The array element in C# can be of any data type. The array subscript starts from 0, that is, the subscript corresponding to the first element is 0, and then increments one by one. Arrays can be one-dimensional or multi-dimensional.
One-dimensional array is the most basic array type, and its declaration method is as follows:
Data type [ ] array name;
For example:
int [ ] anArray ; // Declare an integer one-dimensional array
An array with two dimensions is a two-dimensional array, and its declaration method is as follows:
Data type [ , ] array name;
For example:
int [ , ] anArray ; // Declare an integer two-dimensional array
float [ , ]anArrayOfFloats; // Declare a floating point two-dimensional array
string [ , ] anArrayOfStrings; // Declare a two-dimensional array of strings
When declaring array variables, the array has not been created and no memory space has been allocated for the elements in the array. Therefore, after declaring the array, the array needs to be instantiated:
anArray = new int [2,4] ;
anArrayOfStrings = new stirng [2,4] ;
We can also initialize array elements with the given value.
int [, ] anArray = new int [2, 4] {{1,2,3,4},{5,6,7,8}};
string [, ] anArrayOfStrings = new string [2, 2] {{"A","B"}, {"Champion", "Runner-up" }};
The following shortcuts are also available:
int [, ] anArray = {{0,1,2,3},{1,2,3,4}};
string [, ] anArrayOfStrings = {{"A","B"}, {"Champion", "Runner-up" }};
In C#, arrays provide us with some useful features, and using these features, we can complete some more advanced functions.
Array name.Length: Returns an integer that represents the total number of elements in all dimensions of the array.
Array name.Rank: Returns an integer that represents the dimension of the array.
Array name.GetLength(int dimension): Returns an integer that represents the number of elements in the specified dimension of the array (specified by the parameter dimension, the dimension starts from zero).
A statement runs the embedded statement loop for each element in an array or collection.
The syntax format of the foreach statement is:
foreach (data type identifier in expression)
Embed statements
//A one-dimensional integer array containing 6 elements;
int[] mf1=new int[6]; //Note that the range of the initialization array is initialized, or specify the initial value;
// One-dimensional integer array containing 6 elements, initial values 1, 2, 3, 4, 5, 6
int[] mf2=new int[6]{1,2,3,4,5,6};
//One-dimensional string array, if initial value setting item is provided, the new operator can also be omitted
string[] mf3={"c","c++","c#"};
//One-dimensional object array
Object[] mf4 = new Object[5] { 26, 27, 28, 29, 30 };
//Two-dimensional integer array, initial value mf5[0,0]=1,mf5[0,1]=2,mf5[1,0]=3,mf5[1,1]=4
int[,] mf5=new int[,]{{1,2},{3,4}};
// 6*6 2D integer array
int[,] mf6=new mf[6,6];
Let's look at the traversal of a one-dimensional string array
using System; public class MikeCat { static void PrintArray(string[] arr) { //Print array elements, representing the number of array elementsfor(int i=0;i<;i++) { ("arr[{0}]={1}",i,arr[i]); } } public static void Main() { string[] arr={"c","c++","c#"}; //Pass the array as a parameterPrintArray(arr); } }
Program result: arr[0]=c arr[1]=c++ arr[2]=c#
Let’s take a look at the traversal of an integer array with 4 rows, 2 columns (4*2):
using System; public class MikeCat { static void PrintArray(int[,] arr) { //Transfer the two-dimensional array through two FOR loopsfor(int i=0;i<4;i++)//Initialize i as a loop variable, i++ realizes the autoincrement operation of this variable.// After the for loop meets the conditions, the loop body is executed once and then i++ is executed, and then enters the next loop. Simple C grammar, here is a brief introduction for beginners to learn. (For details, please refer to "c# Advanced Programming 4.0" book){for(int j=0;j<2;j++) { ("arr[{0},{1}]={2}",i,j,arr[i,j]);//Print each 2D array element} } } public static void Main() { //Main function//Pass the array as a parameterPrintArray(new int[,]{{1,2},{3,4},{5,6},{7,8}}; } }
Running result: arr[0,0]=1 arr[0,1]=2 arr[1,0]=3 arr[1,1]=4 arr[2,0]=5 arr[2,1]=6 arr[3,0]=7 arr[3,1]=8