C#, introductory tutorial (12) - basic knowledge of arrays and their use

Previous:

C#, introductory tutorial (11) - basic knowledge and advanced applications of enumeration (Enum) icon-default.png?t=N7T8https://blog.csdn.net/beijinghorn/article/details/123917587

An array is a collection of data, a set of identical data stored in order.

There are several characteristics of arrays to remember:

(1) The values ​​are all of the same data type; they are not limited to integers, real numbers and other values, but can be of many types;

(2) Sequentially read and assign (modify) the values ​​of the array through the loop method;

(3) Access a specified element through the array subscript (index), read, assign (modify);

(4) ) array elements are stored continuously.

(5) C# array subscripts (indexes) start from 0; the smallest subscript (index) 0 corresponds to the first element, and the highest subscript (index) (Length-1) corresponds to the last element.

(6) The total number of data in the array is Length; the two-dimensional array is GetLength(0) and GetLength(1)....

1. Definition of array

// 1维数组
// 定义数组
数据类型[] 数组名;

// 定义数组,并初始化数组中的元素
数据类型[] 数组名 = new 数据类型[长度];
数据类型[] 数组名 = { 值1, 值2, ...};
数据类型[] 数组名 = new 数据类型[长度] { 值1,值2, ...};


// 2维 或 更多维数组
// 定义数组
数据类型[,] 数组名;

// 定义数组,并初始化数组中的元素
数据类型[,] 数组名 = new 数据类型[长度1,长度2];
数据类型[,] 数组名 = { {值1, 值2, ...}, {值1, 值2, ...} ... };
数据类型[,] 数组名 = new 数据类型[长度1,长度2] { {值1, 值2, ...}, {值1, 值2, ...} ... };

for example:

// 定义空的数组
int[] arrayFirst = null;


// 定义并给出初值
int[] arraySecond = new int[3] { 0, 1, 2 };

// 二维数组
int[,] rrayThird = new int[2,2] {
    { 11, 12 },
    { 21, 22 }
};

2. Uses and taboos of arrays

The use of arrays is mainly for value calculation or modification (assignment).

/// <summary>
/// 计算数组的最大值
/// 算法:顺序比较法(谁编的名字?)
/// </summary>
/// <param name="array">数组</param>
/// <returns></returns>
public int GetMaxvalue(int[] array)
{
	// 先设定最大值为很小的值
	// int.MinValue = -2147483648
	int result = int.MinValue;
	for (int i = 0; i < array.Length; i++)
	{
		// 取数组的第 i 个值参与比较
		if (array[i] > result)
		{
			result = array[i];
		}
	}
	return result;
}

Taboos that must be kept in mind when using arrays:

(1) Don’t trust any incoming array! May be null;

(2) The subscripts of arrays often go out of line!

/// <summary>
/// 计算数组的最大值
/// 算法:顺序比较法(谁编的名字?)
/// </summary>
/// <param name="array">数组</param>
/// <returns></returns>
public int GetMaxvalue(int[] array)
{
    // 一定不要相信!
    if(array == null) 
    {
        throw new Exception("Null array!");
    }
	// 先设定最大值为很小的值
	// int.MinValue = -2147483648
	int result = int.MinValue;
	for (int i = 0; i < array.Length; i++)
	{
		// 取数组的第 i 个值参与比较
		if (array[i] > result)
		{
			result = array[i];
		}
	}
	return result;
}

3. Limitations of Arrays

Except for pure mathematics or numerical calculation situations that require operating efficiency, arrays are not used much in actual projects.

The characteristics of arrays are also shackles that limit their use.

List, Dictionary, Queue, Stack, Hashtable, etc. are more commonly used in engineering applications.

It is recommended that you pay attention to and become proficient in these data set types later.

The road is long...

Next:

C#, introductory tutorial (13) - basic knowledge of characters (char) and strings (string) icon-default.png?t=N7T8https://blog.csdn.net/beijinghorn/article/details/123928151

Guess you like

Origin blog.csdn.net/beijinghorn/article/details/123918227