Home  >  Article  >  Backend Development  >  Detailed explanation of c# array

Detailed explanation of c# array

黄舟
黄舟Original
2016-12-17 17:33:271272browse

Array is a data structure, which is declared as follows:

type[] arrayName;

Array has the following properties:

1. Arrays can be one-dimensional, multi-dimensional or interleaved.
2. The default value of numeric array elements is set to zero, while the default value of reference elements is set to null.
3. A jagged array is an array of arrays, therefore, its elements are reference types and are initialized to null.
4. The index of an 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

//声明一维数组,没有初始化,等于null 
  int[] intArray1; 
  //初始化已声明的一维数组 
  intArray1 = new int[3]; //数组元素的默认值为0 
  intArray1 = new int[3]{1,2,3}; 
  intArray1 = new int[]{1,2,3}; 
   
 
  //声明一维数组,同时初始化 
  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"};

Multi-dimensional array

//声明二维数组,没有初始化 
 short[,] sArray1; 
 //初始化已声明的二维数组 
 sArray1 = new short[2,2]; 
 sArray1 = new short[2,2]{{1,1},{2,2}}; 
 sArray1 = new short[,]{{1,2,3},{4,5,6}}; 
  
 //声明二维数组,同时初始化 
 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}}; 
 //声明三维数组,同时初始化 
 byte[,,] bArray1 = {{{1,2},{3,4}},{{5,6},{7,8}}};

Jagged array

//声明交错数组,没有初始化 
 int[][] JagIntArray1; 
 //初始化已声明的交错数组 
 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 //使用int[]数组变量 
     }; 
 //声明交错数组,同时初始化 
 int[][] JagIntArray2 = { 
     new int[]{1,1,1}, 
     //new int []{2,2}, 
     intArray1 
     };

An array is a group of elements of the same type that are grouped together, using a common name, and accessed through assigned subscripts.

An array is a set of data of the same type. When accessing data in an array, you can specify it by subscripting. Array elements 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 increases one by one thereafter. Arrays can be one-dimensional or multi-dimensional.

One-dimensional array is the most basic array type. Its declaration method is as follows:

Data type [ ] array name;

Example:
int [ ] anArray ; // Declare a one-dimensional array of integer type

with two An array with two dimensions is a two-dimensional array, and its declaration method is as follows:
Data type [ , ] array name;
Example:

int [ , ] anArray ; // Declare a two-dimensional array of integer
float [ , ]anArrayOfFloats ; // Declare a two-dimensional array of floating point type
string [ , ] anArrayOfStrings; // Declare a two-dimensional array of string type

When declaring an array variable, the array has not been created and no elements have been allocated to the elements in the array. Memory space, therefore, after declaring the array, you need to instantiate the array:

anArray = new int [2,4] ;
anArrayOfStrings = new stirng [2,4] ;

We can also use the given value to array element is initialized.

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" }};

You can also use the following shortcuts:

int [, ] anArray = {{0,1,2, 3},{1,2,3,4}};
string [, ] anArrayOfStrings = {{"A","B"}, {"champion","runner-up"}};

in C# language , arrays provide us with some useful features, 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 representing 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).

4. The foreach statement runs the embedded statement in a loop for each element in the array or collection.
The syntax format of the foreach statement is:
foreach (data type identifier in expression)
Embedded statement

//One-dimensional integer array containing 6 elements;
int[] mf1=new int[6]; // Pay attention to the range of the initialized array, or specify the initial value;

//One-dimensional integer array containing 6 elements, initial value 1, 2, 3, 4, 5, 6
int[] mf2=new int[6]{ 1,2,3,4,5,6};

//One-dimensional string array, if an initializer is provided, you can also omit the new operator
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 two-dimensional integer array
int[,] mf6=new mf[6,6];

Let’s look at a one-dimensional string Array traversal

using System; 
public class MikeCat 
{ 
static void PrintArray(string[] arr) 
{ 
//打印数组元素,arr.Length 表示数组元素的个数 
for(int i=0;i<arr.Length;i++) 
{ 
Console.WriteLine("arr[{0}]={1}",i,arr[i]); 
} 
} 
public static void Main() 
{ 
string[] arr={"c","c++","c#"}; 
//将数组作为一个参数传递 
PrintArray(arr); 
} 
}

Program result: arr[0]=c arr[1]=c++ arr[2]=c#

Let’s look at the traversal of an integer array with 4 rows and 2 columns (4*2):

using System; 
public class MikeCat 
{ 
static void PrintArray(int[,] arr) 
{ 
//通过两次FOR循环遍历二维数组 
for(int i=0;i<4;i++)//初始化i作为循环变量,i++实现该变量的自增运算。 
//for循环满足条件后执行完循环体一次后执行i++,然后进入下次循环。简单的c语法,这里做简单介绍供初学者学习。(详细可参阅《c# 高级编程4.0》一书) 
{ 
for(int j=0;j<2;j++) 
{ 
Console.WriteLine("arr[{0},{1}]={2}",i,j,arr[i,j]);//打印每个二维数组元素 
} 
} 
} 
public static void Main() 
{ 
//主函数 
//将数组作为一个参数传递 
PrintArray(new int[,]{{1,2},{3,4},{5,6},{7,8}}; 
} 
}

The above is the detailed explanation of c# arrays. For more related articles, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn