当前位置:网站首页>Use of C array
Use of C array
2022-04-22 07:50:00 【Dream back to Datang knock code】
Array
Definition of array :
A collection that can store multiple data of the same type , Once the length of the array is determined, it cannot be changed
Gets the length of the array :
Array name .Lenght
Elements of array :
Each value in the array is called an element , Elements are separated by commas
Index of the array :
All elements in the array have subscripts , from 0 Start , The first element is 0, The subscript of the following element is the subscript of the previous element plus 1
Subscript is the required value to get the array element
Be careful :
Array subscripts must not exceed the range of subscripts , If the subscript does not exist in the array , It will report an exception that the array is out of bounds
Declaration of arrays :
<1> The first way to declare an array
An array type [] Array name ={ Elements 1, Elements 2 , ... };
Declare an array and assign a value to it directly
int[] _arr = {12,45,23,-33,21 };
Know the type of array , And know the value of the array
<2> The second way to declare an array
An array type [] Array name = new An array type [ The length of the array ];
Declare an array , Array has 10 Elements , Because there is no assignment , So the default value of the elements in the array is 0;
int[] _arr1 =
new
int[10];
Know the type of array , And know the array length
Value and assignment of array :
assignment
Array name [ Subscript ]= value ;
int[] _arr1 =
new
int[10];
Is an array _arr1 assignment Assign a value to the array by subscript , The value is also obtained by subscript
_arr[0] = 34;
_arr[1] = 27;
_arr[2] = 23;
Value
Array name [ Subscript ];
int _num1 = _arr[0];
int _num2 = _arr[1];
int _num3 = _arr[2];
Code example of using array :
using System;
namespace Chapter 11 use of arrays
{
class
Program
{
static
void Main(
string[] args)
{
// Use of arrays
#region 1. Get the maximum and minimum values of the elements in the integer array , And sum up
int[] _arr1 = { 23,56,12,87,-2,3,-5 };
// Defines the maximum value of the variable, which is used to receive the maximum value in the array , And give the maximum int Minimum value in type
// In this way, as long as there is one greater than the maximum value, it will be assigned to him
int maxNum =
int.MinValue;
//int.MinValue yes int Minimum value in type
// Defines the minimum value of the variable, which is used to receive the minimum value in the array , And give the minimum value int Maximum in type
// In this way, as long as there is one smaller than the minimum value, it will be assigned to him
int minNum =
int.MaxValue;
//int.MaxValue yes int Maximum in type
// Definitions and , Used to record elements and add them
int numHe = 0;
// Use foreach Traversal array
foreach (var item
in _arr1)
{
// For maximum , Just compare maxNum If the value is large, assign the value to maxNum (maxNum Now it's int Minimum value in type )
if (maxNum<item)
{
maxNum = item;
}
// minimum value , Just compare minNum If the value is small, assign the value to minNum (minNum Now it's int Maximum in type )
if (minNum >item )
{
minNum = item;
}
// Sum up
numHe += item;
}
Console.WriteLine(
" The maximum is :"+maxNum+
"\n The minimum is :"+minNum+
"\n And is :"+numHe);
#endregion
#region Invert the order of the elements in the array
string[] _str1 = {
" Li Bai ",
" Wang wei ",
" Du Fu ",
" Xi Shi ",
" The sable cicada ",
" Jade bracelet " ,
" Little Joe "};
// Invert elements in an array 0 1 2 3 4 5
// The inverted subscript is 5 4 3 2 1 0
// that The principle of exchange is 0 - 5 1 - 4 That is to say
// The current element subscript and the array length minus the current element minus 1 Subscript exchange
// Define temporary variables to store array elements
string _stemp;
// Because two elements are exchanged at a time, the number of cycles is divided by 2
for (
int i = 0; i < _str1.Length/2; i++)
{
// Set the first of the array i Take out the elements and save them temporarily
_stemp = _str1[i];
// Give array i Element assignment
// Because the length of the array is 6, So when you subtract the subscript of the current element, you also subtract 1
_str1[i] = _str1[_str1.Length - i - 1];
// Assign values to the following elements
_str1[_str1.Length - i - 1] = _stemp;
}
// Loop print array
for (
int i = 0; i < _str1.Length ; i++)
{
Console.WriteLine(_str1[i] );
}
#endregion
}
}
}
版权声明
本文为[Dream back to Datang knock code]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220621527039.html
边栏推荐
猜你喜欢
随机推荐
unity小游戏及相关面试题
JS - 精度问题
C#集合
可视化编程——实验二
【TCP/IP 五 ICMP】
Dependency conflict finding and resolution (taking easypoi as an example, nosuchmethoderror or NoClassDefFoundError appears)
unity面试题
Set method and get method
Unity 知识点(常用核心类)
CSharp type conversion
ES6 modularization and promise
unity面试题
【GPS - NMEA-0183协议】
C#数组的使用
多线程(线程通信【生产者与消费者】)
Oracle 序列使用整理
A thrilling redis vulnerability to the principle of server target changing
运行程序~自定义类似CMD命令打开非系统软件
接口的应用与定义
C-10 年龄问题

![Rt-thread [一] 创建工程](/img/cd/9a2a9c7381caaf150b3700e348e38a.png)






![Stm32外设篇 [三] 串口 RS232 RS485](/img/47/d8c8bbe67559d01cde7a118d2dde78.png)
