[英]Why we use -1 within a for Loop c#
I have code which checks if a word if a palindrome or not.我有代码可以检查一个单词是否是回文。 Within the for loop there is a -1 value.
在 for 循环中有一个 -1 值。 Can someone explain to me why -1 is used after the name.Length in c#
有人可以向我解释为什么在 c# 中的 name.Length 后使用 -1
public static void Main()
{
string name = "Apple";
string reverse = string.Empty;
for (int i = name.Length - 1; i >= 0; i--)
{
reverse +=name[i];
}
if (name == reverse)
{
Console.WriteLine($"{name} is palindrome");
}else
{
Console.WriteLine($"{name} is not palindrome");
}
That's because whoever wrote the code, wanted to write:那是因为无论谁写了代码,都想写:
reverse += name[i];
String operator [] takes values from 0 upto (string's length-1).字符串运算符 [] 取值从 0 到(字符串的长度为 1)。 If you pass length or more, you will get an exception.
如果你通过长度或更多,你会得到一个例外。 So, code's author had to ensure that i==Length won't be ever passed there.
因此,代码的作者必须确保 i==Length 永远不会在那里传递。 So it starts from Length-1 and counts downwards.
所以它从 Length-1 开始向下计数。
Also, note that the other bound of i is 0 ( >=
, not >
, so 0 is included), so the loop visits all values from 0 to length-1, so it visits all characters from the string.另外,请注意 i 的另一个边界是 0(
>=
,而不是>
,因此包括 0),因此循环访问从 0 到 length-1 的所有值,因此它访问字符串中的所有字符。 Job done.任务完成。
However, it doesn't have to be written in that way.但是,它不必以这种方式编写。 The only thing is to ensure that the string operator [] wont see values of of its range.
唯一的事情是确保字符串运算符 [] 不会看到其范围的值。 Compare this loop, it's identical in its results:
比较这个循环,结果是一样的:
for (int i = name.Length; i >= 1; i--)
{
reverse += name[i-1];
}
Note that I also changed 0 to 1.请注意,我也将 0 更改为 1。
Of course, it's also possible to write a loop with the same effects in a lot of other ways.当然,也可以通过许多其他方式编写具有相同效果的循环。
The first element in an array is at the index 0 ( array[0]
).数组中的第一个元素位于索引 0 (
array[0]
) 处。 Because the indexing starts at 0 instead of 1 it means that the final element in the array will be at index array.Length-1
.因为索引从 0 而不是 1 开始,这意味着数组中的最后一个元素将位于索引
array.Length-1
处。
If you had the word and then your array would look like:如果你有这个词,那么你的数组将如下所示:
name[0] = 'a'
name[1] = 'n'
name[2] = 'd'
the name.Length
would equal 3. As you can see, there isn't an element at index 3 in the array so you need to subtract 1 from the length of the array to access the last element. name.Length
将等于 3。如您所见,数组中索引 3 处没有元素,因此您需要从数组的长度中减去 1 才能访问最后一个元素。
The for loop in your example starts with the last element in the array (using i as the index).您示例中的 for 循环从数组中的最后一个元素开始(使用 i 作为索引)。 If you tried to set
i
to i = name.Length
then you would get an index out of bounds error because there isn't an element at the position name.Length
.如果您尝试将
i
设置为i = name.Length
,那么您会得到一个索引越界错误,因为 position name.Length
处没有元素。
String operator [] takes values from 0. The first element in an string is at the index 0, so we need to adjust by subtracting one.字符串运算符 [] 从 0 中取值。字符串中的第一个元素位于索引 0 处,因此我们需要通过减一来进行调整。
For Example:例如:
string str = "test";
int length = str.length; //Length of the str is 4. (0 to 3)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.