如何循环处理一个 C# 数组?
为了循环处理 C# 中的数组,请使用任何一个循环。这些循环具有设定的起始和结束值,允许你在迭代中设定或检查值。
C# 具有 while、do…while、for 和 foreach 循环来循环处理数组。
让我们在 C# 中看一个 for 循环的例子 −
示例
using System;
namespace ArrayApplication {
class MyArray {
static void Main(string[] args) {
int [] n = new int[10];
int i,j;
for ( i = 0; i < 10; i++ ) {
n[ i ] = i + 10;
}
for (j = 0; j < 10; j++ ) {
Console.WriteLine("Element[{0}] = {1}", j, n[j]);
}
Console.ReadKey();
}
}
}现在让我们看看上面如何循环处理数组的。
10 个整数的数组 −
int [] n = new int[10];
现在,初始化上面声明的数组的元素 −
for ( i = 0; i < 10; i++ ) {
n[ i ] = i + 10;
}以上的循环从 i=0 到 i = 10 进行迭代,在每次迭代之后 i 的值递增 −
i++;
在每次从 i = 10 的迭代中,该值被添加到数组中,起始元素为 10 −
n[ i ] = i + 10;
输出
Element[0] = 10 Element[1] = 11 Element[2] = 12 Element[3] = 13 Element[4] = 14 Element[5] = 15 Element[6] = 16 Element[7] = 17 Element[8] = 18 Element[9] = 19
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
JavaScript
PHP