Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 需要帮助理解数组中的语法吗_C#_Arrays_For Loop - Fatal编程技术网

C# 需要帮助理解数组中的语法吗

C# 需要帮助理解数组中的语法吗,c#,arrays,for-loop,C#,Arrays,For Loop,我很难理解for循环在做什么 对我来说,我认为这是: int i = 0; //Declaring i to become 0. i is the value in myArray? i < myArray.Length; //When i is less than any value in myArray keep looping? i++; //Every time this loop goes through increase i by 1? inti=0//声明我将成为0。i

我很难理解
for
循环在做什么

对我来说,我认为这是:

int i = 0; //Declaring i to become 0. i is the value in myArray?

i < myArray.Length; //When i is less than any value in myArray keep looping?

i++; //Every time this loop goes through increase i by 1?
inti=0//声明我将成为0。i是myArray中的值?
i

//制作一个名为myArray的数组,其中包含20,5,7,2,55
int[]myArray={20,5,7,2,55};
//使用内置功能Array.Sort();整理myArray
Sort(myArray);
for(int i=0;i
i
这不是针对myArray中的值进行测试,而是针对长度(数组包含多少项)进行测试。因此它的意思是:当i小于数组的长度时

因此循环将继续进行,每次循环时向i加1(正如您正确所说的),当i等于数组的长度时,意味着它已经遍历了所有值,它将退出循环

正如Nicolás Straub指出的,i是数组的索引,意思是数组中某个项的位置,您已使用值0对其进行了初始化,这是正确的,因为数组中的第一个值的索引为0

要直接回答有关for循环的问题,请执行以下操作:

for循环以迭代方式(多次)执行代码行,数量取决于其控制语句:

for (int i = 0; i < myArray.Length; i++)
for(int i=0;i

For循环通常是先决条件(循环的条件在代码之前),并且有循环计数器,即i(i实际上是一个计数器,但可以被视为索引,因为您要遍历每个元素,如果您想跳过一些,那么我只会是一个计数器)。当你知道在开始循环之前要循环多少次时,For是非常有用的。

你的想法是正确的,除非其他人已经说过。将数组视为数据序列。甚至可以使用Reverse()方法将其应用于数组。我会研究更多关于数组的内容,以便您了解可以使用数组执行的不同操作,最重要的是,如果您需要在控制台、列表框或gridview中从文本或csv文件中读取或写入数组,则会了解这些操作

我建议你加上:

Console.ReadLine()

执行此操作时,应用程序将如下所示: 2. 5. 7. 20
55

我将对您的编程知识做出一些假设,因此请原谅,如果这个解释涵盖了您已经熟悉的主题,但它们对于理解for循环的功能、用途以及当有人来到您身后阅读您的代码时语义将是什么都很重要。你的问题表明你已经非常接近于理解它了,所以希望一旦你有了一个好的解释,它会像一吨砖头一样击中你

考虑长度为5的字符串数组。您可以用C#对其进行初始化,如下所示:

这意味着您有一个数组,它为字符串分配了5个插槽。这些插槽的名称是数组的索引。不幸的是,对于像您这样的编程新手来说,索引从0开始(这称为零索引),而不是从1开始。这意味着我们新的
字符串[]
中的第一个插槽的名称或索引为0,第二个为1,第三个为3,依此类推。这意味着数组的长度将始终是一个数字,等于最后一个插槽的索引加上一个;换言之,因为数组是0索引的,第一个(第一)时隙的索引是0,所以我们知道任何给定时隙的索引是“代码>N-1</CODE>><代码> n>代码>,而那些不是程序员(或崭露头角的程序员)的人通常会被认为是数组中的那个槽的位置。

我们可以使用索引从与索引对应的插槽中的数组中选择值。以您的例子:

int[] myArray = { 20, 5, 7, 2, 55 };

bool first = myArray[0] == 20: //=> true
bool second = myArray[1] == 5; //=> true
bool third = myArray[2] == 7; //=> true
// and so on...
因此,您可以看到,我们传递到中的数字(方括号<代码>[]
)对应于我们试图访问的数组中的位置

<>代码> C语法语言中的循环(C是C、C++、java、JavaScript和其他一些语言之一)通常遵循相同的约定,用于“参数”:

要了解这些字段的预期用途,了解索引是什么很重要。索引可以看作是数组中每个插槽(或列表或类似列表的任何内容)的名称或位置

因此,为了解释for循环的每一部分,让我们逐一介绍它们:

索引初始值设定项 因为我们将使用索引来访问数组中的插槽,所以需要将其初始化为for循环的起始值。for循环语句中第一个分号之前的所有内容将在for循环中的任何其他内容运行之前恰好运行一次。我们将这里初始化的变量称为索引,因为它跟踪for循环寿命范围内的当前索引。对于带有嵌套循环的索引,使用拉丁字母表的后续字母将该变量命名为
i
,这是典型的做法(因此也是良好的做法)。正如我所说,这个初始化语句只发生一次,因此我们将0指定给
I
,表示我们希望在数组的第一个元素上开始循环

条件 声明for循环时发生的下一件事是检查条件。此检查将是每次循环运行时运行的第一项检查,如果检查返回
false
,循环将立即停止。这种情况可以是任何情况,只要它导致
bool
。如果有一个特别复杂的for循环,可以将条件委托给方法调用:

for (int i = 0; ShouldContinueLooping(i); i++)
string[] arr = new string[5];
int[] myArray = { 20, 5, 7, 2, 55 };

bool first = myArray[0] == 20: //=> true
bool second = myArray[1] == 5; //=> true
bool third = myArray[2] == 7; //=> true
// and so on...
for (index_initializer; condition; index_incrementer)
for (int i = 0; ShouldContinueLooping(i); i++)
// Making an array called myArray that contains 20,5,7,2,55
int[] myArray = { 20, 5, 7, 2, 55 };

// Using the built in feature, Array.Sort(); to sort out myArray
Array.Sort(myArray);
// Array is now [2, 5, 7, 20, 55]

for (int i = 0; i < myArray.Length; i++)
{
    int currentNumber = myArray[i];
    Console.WriteLine($"Index {i}; Current number {currentNumber}");
}
Index 0; Current number 2
Index 1; Current number 5
Index 2; Current number 7
Index 3; Current number 20
Index 4; Current number 55
for (int i = 0; i < myArray.Length; i++)
{
    Console.WriteLine(myArray[i]);
}
int i = 0;
while (i < myArray.Length)
{
    Console.WriteLine(myArray[i]);
    i++;
}
int i = 0;
START:
if (i < myArray.Length)
  goto BODY;
else
  goto END;
BODY:
Console.WriteLine(myArray[i]);
i++;
goto START;
END:
// the rest of your program here.