当前位置:网站首页>July 31, 2022 -- Take your first steps with C# -- Use arrays and foreach statements in C# to store and iterate through sequences of data
July 31, 2022 -- Take your first steps with C# -- Use arrays and foreach statements in C# to store and iterate through sequences of data
2022-08-01 10:02:00 【DXB2021】
Process related sequences of data in a data structure called arrays. 然后,Learn how to iterate through each item in a sequence.
简介
C# Arrays can store sequences of values in a single data structure. 换而言之,Suppose a variable that can hold multiple values. Once you have a variable that can store all the values,to sort the values、Reverse the order of values、Iterate over each value and check each value one by one,等等.
练习-Array basic information
什么是数组?
An array is a series of individual data elements accessible through a single variable name. Access each element in the array using a zero-based numeric index.
声明数组
Arrays are a special type of variable,Multiple values of the same data type can be saved. Because the data type and size of the array must be specified at the same time,So the declaration syntax is slightly different.
步骤1-声明新数组
new The operator creates a new instance of the array in computer memory that can hold three string values.
string[] fraudulentOrderIDs = new string[3];为数组的元素赋值
此时,We have declared an array of strings,But every element of that array is empty. To access elements of an array,Please use a zero-based numeric index within square brackets.
like regular variables,使用 = Assign values to assign values.
步骤2-为数组中的元素赋值
string[] fraudulentOrderIDs = new string[3];
fraudulentOrderIDs[0] = "A123";
fraudulentOrderIDs[1] = "B456";
fraudulentOrderIDs[2] = "C789";步骤3-Attempt to use an index outside the bounds of the array
Access each element in the array starting at zero.
string[] fraudulentOrderIDs = new string[3];
fraudulentOrderIDs[0] = "A123";
fraudulentOrderIDs[1] = "B456";
fraudulentOrderIDs[2] = "C789";
fraudulentOrderIDs[3] = "D000";运行应用时,Received a long error message.
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.IndexOutOfRangeException: Index was outside the bounds of the array.Get the value of an element in an array
Values can be obtained from elements of an array in the same way. Use the element's index to retrieve the element's value.
步骤4-Retrieve the value of an array
string[] fraudulentOrderIDs = new string[3];
fraudulentOrderIDs[0] = "A123";
fraudulentOrderIDs[1] = "B456";
fraudulentOrderIDs[2] = "C789";
// fraudulentOrderIDs[3] = "D000";
Console.WriteLine($"First: {fraudulentOrderIDs[0]}");
Console.WriteLine($"Second: {fraudulentOrderIDs[1]}");
Console.WriteLine($"Third: {fraudulentOrderIDs[2]}");First: A123
Second: B456
Third: C789步骤5-Reassign the value of the array
string[] fraudulentOrderIDs = new string[3];
fraudulentOrderIDs[0] = "A123";
fraudulentOrderIDs[1] = "B456";
fraudulentOrderIDs[2] = "C789";
// fraudulentOrderIDs[3] = "D000";
Console.WriteLine($"First: {fraudulentOrderIDs[0]}");
Console.WriteLine($"Second: {fraudulentOrderIDs[1]}");
Console.WriteLine($"Third: {fraudulentOrderIDs[2]}");
fraudulentOrderIDs[0] = "F000";
Console.WriteLine($"Reassign First: {fraudulentOrderIDs[0]}");First: A123
Second: B456
Third: C789
Reassign First: F000初始化数组
Just like when you declare a variable you can initialize it,You can use a special syntax with curly braces when declaring a new array,初始化新的数组.
步骤6-初始化数组
/*
string[] fraudulentOrderIDs = new string[3];
fraudulentOrderIDs[0] = "A123";
fraudulentOrderIDs[1] = "B456";
fraudulentOrderIDs[2] = "C789";
// fraudulentOrderIDs[3] = "D000";
*/
string[] fraudulentOrderIDs = { "A123", "B456", "C789" };
Console.WriteLine($"First: {fraudulentOrderIDs[0]}");
Console.WriteLine($"Second: {fraudulentOrderIDs[1]}");
Console.WriteLine($"Third: {fraudulentOrderIDs[2]}");
fraudulentOrderIDs[0] = "F000";
Console.WriteLine($"Reassign First: {fraudulentOrderIDs[0]}");First: A123
Second: B456
Third: C789
Reassign First: F000获取数组大小
要确定数组的大小,可使用 Length 属性.
步骤7-使用数组的Lengthproperty to print the number of the fake order
string[] fraudulentOrderIDs = { "A123", "B456", "C789" };
Console.WriteLine($"First: {fraudulentOrderIDs[0]}");
Console.WriteLine($"Second: {fraudulentOrderIDs[1]}");
Console.WriteLine($"Third: {fraudulentOrderIDs[2]}");
fraudulentOrderIDs[0] = "F000";
Console.WriteLine($"Reassign First: {fraudulentOrderIDs[0]}");
Console.WriteLine($"There are {fraudulentOrderIDs.Length} fraudulent orders to process.");First: A123
Second: B456
Third: C789
Reassign First: F000
There are 3 fraudulent orders to process.概括
使用数组时,The following important things should be kept in mind.
- 数组是一种特殊的变量,Used to hold a series of related data elements.
- The basic format of array variable declarations should be kept in mind.
- 访问数组的每个元素,Use the zero-based index inside square brackets to set or get its value.
- If trying to access an index outside the bounds of the array,则会出现运行时异常.
Lengthproperty to programmatically determine the element number in an array.
练习-foreach语句
使用foreach遍历数组
foreach The statement iterates over each element in the array.
string[] names = { "Bob", "Conrad", "Grant" };
foreach (string name in names)
{
Console.WriteLine(name);
}在 foreach 关键字下,包含 Console.WriteLine(name); The code block will be names Executed once for each element of the array. 当 .Net When iterating over each element of the array at runtime,将存储在 names The value in the current element of the array is assigned to a temporary variable name,for easy access to code blocks.
Bob
Conrad
Grant步骤1-创建并初始化int数组
int[] inventory = { 200, 450, 700, 175, 250 };步骤2-添加foreachstatement to iterate through the array
int[] inventory = { 200, 450, 700, 175, 250 };
foreach (int items in inventory)
{
}步骤3-Add a variable to sum the value of each element in the array
int[] inventory = { 200, 450, 700, 175, 250 };
int sum = 0;
foreach (int items in inventory)
{
sum += items;
}步骤4-Displays the final value of the sum
int[] inventory = { 200, 450, 700, 175, 250 };
int sum = 0;
foreach (int items in inventory)
{
sum += items;
}
Console.WriteLine($"We have {sum} items in inventory.");We have 1775 items in inventory.步骤5-创建一个变量,Used to store the current bucket number and display subtotals
int[] inventory = { 200, 450, 700, 175, 250 };
int sum = 0;
int bin = 0;
foreach (int items in inventory)
{
sum += items;
bin++;
Console.WriteLine($"Bin {bin} = {items} items (Running total: {sum})");
}
Console.WriteLine($"We have {sum} items in inventory.");Bin 1 = 200 items (Running total: 200)
Bin 2 = 450 items (Running total: 650)
Bin 3 = 700 items (Running total: 1350)
Bin 4 = 175 items (Running total: 1525)
Bin 5 = 250 items (Running total: 1775)
We have 1775 items in inventory.概括
about this unit foreach Statements and other content,The following points need to be kept in mind.
- 使用
foreachstatement to iterate through each element in the array,Executes the associated block of code once for each element in the array. foreachThe statement sets the value of the current element in the array to a temporary variable,This temporary variable can be used in the body of the code block.- 使用
++Increment operator will 1 to the current value of the variable.
挑战
To determine whether an element begins with a letter“B”开头,请使用 Stri
string[] orderIDs = { "B123", "C234", "A345", "C15", "B177", "G3003", "C235", "B179" };
foreach (string orderID in orderIDs)
{
if (orderID.StartsWith("B"))
{
Console.WriteLine(orderID);
}
}ng.StartsWith() 方法. Check out the following simple example,了解如何使用 String.StartsWith() 方法,to use it in your code:
string name = "Bob";
if (name.StartsWith("B"))
{
Console.WriteLine("The name starts with 'B'!");
}The name starts with 'B'!解决方案
string[] orderIDs = { "B123", "C234", "A345", "C15", "B177", "G3003", "C235", "B179" };
foreach (string orderID in orderIDs)
{
if (orderID.StartsWith("B"))
{
Console.WriteLine(orderID);
}
}B123
B177
B179边栏推荐
猜你喜欢
随机推荐
pve 删除虚拟机「建议收藏」
C#/VB.NET convert PPT or PPTX to image
安装GBase 8c数据库的时候,报错显示“Resource,如何解决?
CTFshow,命令执行:web33
Get the Token from the revised version of Qubutu Bed
【云驻共创】分布式技术之华为云全域调度技术与实践
Naive Bayes--Study Notes--Basic Principles and Code Implementation
Android 安全与防护策略
DBPack SQL Tracing 功能及数据加密功能详解
mysql在cmd的登录及数据库与表的基本操作
2022年7月31日--使用C#迈出第一步--使用C#中的数组和foreach语句来存储和循环访问数据序列
Lsky Pro 企业版手动升级、优化教程
notes....
STM32个人笔记-程序跑飞
ClickHouse多种安装方式
Meeting OA (Upcoming Meetings & All Meetings)
YOLOv7-Pose尝鲜,基于YOLOv7的关键点模型测评
WLAN networking experiment of AC and thin AP
解决new Thread().Start导致高并发CPU 100%的问题
什么是步进电机?40张图带你了解!



![ASP.NET Core 6框架揭秘实例演示[30]:利用路由开发REST API](/img/b3/0167c22f14b97eb0206696495af7b5.png)





