[英]“Index was out of range exception” error
I'm trying to find the first missing positive integers in an array and i keep on getting this error message. 我试图在数组中查找第一个缺少的正整数,并且继续收到此错误消息。 I'm quite new to programming and i can't figure out the reason for this.
我对编程很陌生,我不知道原因。 Can anyone help me find the problem?
谁能帮我发现问题? Also, when the input is [1], it also throws an exception.
同样,当输入为[1]时,它也会引发异常。
This is my code: 这是我的代码:
public static int solution(int[] A)
{
Array.Sort(A);
List<int> missing = new List<int>();
List<int> An = new List<int>();
foreach (var r in A)
{
if (r > 0)
{
An.Add(r);
}
}
int lengthList = (An[An.Count]) - (An[0]);
for (int i = An[0]; i <= lengthList - 1; i++)
{
if (An[i + 1] != An[i] + 1)
{
missing.Add(An[i] + 1);
An.Insert(i + 1, An[i] + 1);
}
}
if (missing != null)
{
return missing[0];
}
else
return 0;
}
You have several flaws in your code. 您的代码中有几个缺陷。 The most important is
最重要的是
int lengthList = (An[An.Count]) - (An[0]);
What should should that be? 那应该是什么? The length of
An
- the number of items in An
you can iterate over - is given by An.Count
. 长度
An
-在项目数An
,你可以遍历-由下式给出An.Count
。
And since lists are 0-indexed, your index i
should walk from 0
( not An[0]
) to An.Count-1
. 并且由于列表是0索引的,所以您的索引
i
应该从0
( 不是An[0]
)移到An.Count-1
。 So your code should look something like this: 因此,您的代码应如下所示:
for (int i = 0; i < An.Count; i++) {
if (An[i + 1] != An[i] + 1) {
missing.Add(An[i] + 1);
An.Insert(i + 1, An[i] + 1); // this will break your algorithm
}
i<An.Count
avoids accessing the index after the last item. i<An.Count
避免访问最后一项之后的索引。
Note that this only fixes this IndexOutOfRangeException
. 请注意,这只能修复此
IndexOutOfRangeException
。 I don't know if the algorithm does what you want at all. 我不知道该算法是否可以完全满足您的要求。 You insert elements to
An
while iterating over it (line An.Insert...
). 您可以在遍历
An
同时向An
插入元素( An.Insert...
行)。 This seems wrong to me. 对我来说这似乎是错误的。
You are having a off-by-one error: 您遇到一个错误的错误:
In your for
loop you use this exit condition: 在
for
循环中,使用以下退出条件:
i <= lengthList - 1
ON a list with for example 10 items that would iterate until 9. 在列表中,例如,有10个项目将迭代到9。
Then you access the array like this: 然后,您可以像这样访问数组:
An[i + 1]
In the given example you would access An[10] which does not exist in a 10-item array/list. 在给定的示例中,您将访问10项数组/列表中不存在的An [10]。
Your correct exit statement for the loop is: 您对循环的正确退出语句是:
i < lengthList - 1
Yur calculation of lengthList
seems to be also wrong. 您对
lengthList
计算似乎也是错误的。 Change it to: 更改为:
int lengthList = An.Count;
Your initial code 您的初始代码
int lengthList = An[An.Count] - An[0];
...gives you also an ArgumentOutOfRangeException
. ...还为您提供了
ArgumentOutOfRangeException
。 It also makes no sense. 这也没有道理。 You are accessing the content of the list, not its length .
您正在访问列表的内容 ,而不是其长度 。
Sidenote: 边注:
Your inital loop condition 您的初始循环状况
int i = An[0]
...is probably also wrong. ...可能也是错误的。 Did you mean this?
你是这个意思吗
int i = 0
your problem code: 您的问题代码:
1) (An[An.Count]) fix that: An[An.Count-1] 1)(An [An.Count])解决了以下问题:An [An.Count-1]
2) int lengthList = (An[An.Count]) - (An[0]); 2)int lengthList =(An [An.Count])-(An [0]); fix that: int lengthList = An.Count;
修复以下问题:int lengthList = An.Count;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.