C# 指数数组的边界之外。C#
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16160428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Index was outside the bounds of the array. C#
提问by Christy
i have a code for Genetic algorithm's Roullete Wheel. my program shows error when my iteration is more than 1000 iterations but it works very good when the iteration less than 1000. here is my code
我有遗传算盘的代码。当我的迭代次数超过 1000 次时,我的程序显示错误,但当迭代次数少于 1000 次时,它运行良好。这是我的代码
private double[] roulleteWheel(double[] nilaiFitnessRil)
{
double[] resultRW = new double[nilaiFitnessRil.GetLength(0)];
double[] probKromosom = new double[nilaiFitnessRil.GetLength(0)];
double[] probKumulatif = new double[nilaiFitnessRil.GetLength(0)];
double pilih=0;
Random random = new Random();
double rnd;
double total = 0;
double temp = 0;
//count total fitness
for (int i = 0; i < nilaiFitnessRil.Length; i++)
{
total += nilaiFitnessRil[i];
}
listBox1.Items.Add(string.Format("total fitness adalah {0}",total));
//count probability for each chromosome
listBox1.Items.Add("result of probabilty for each chromosome");
for (int i = 0; i < nilaiFitnessRil.Length; i++)
{
probKromosom[i] = Math.Round(nilaiFitnessRil[i] / total, 4);
listBox1.Items.Add(probKromosom[i].ToString());
}
//count cumulative probability
listBox1.Items.Add("result of cumulative probabilty ");
for (int i = 0; i < probKromosom.Length; i++)
{
temp += probKromosom[i];
probKumulatif[i] = temp;
listBox1.Items.Add(probKumulatif[i].ToString());
}
//selecting a chromosome by its cumulative probability with a random value
listBox1.Items.Add(" roullete wheel");
for (int n = 0; n < resultRil.Length; n++)
{
rnd = random.NextDouble() * 1.0 - 0.0;
//listBox1.Items.Add(rnd.ToString());
for (int i = 0; i < probKumulatif.Length; i++)
{
//this is where the Index was outside the bounds of the array appear
if (probKumulatif[i] <= rnd && probKumulatif[i + 1] > rnd )
{
pilih = resultRil[i + 1];
}
else if ( rnd <= probKumulatif[0])
{
pilih = resultRil[0];
}
}
resultRil[n] = pilih;
resultRW[n] = resultRil[n];
}
PrintArray(resultRW, listBox1);
return resultRW;
}
this is where the program was terminated cause by Index was outside the bounds of the array
这是由于索引超出数组范围而导致程序终止的地方
if (probKumulatif[i] <= rnd && probKumulatif[i + 1] > rnd )
{
pilih = resultRil[i + 1];
}
else if ( rnd <= probKumulatif[0])
{
pilih = resultRil[0];
}
采纳答案by Daniel Imms
You're getting an index out of bounds because you're referring to i + 1within the loop when you are looping through every item. Therefore one the last iteration you will attempt to access probKumulatif[probKumulatif.Length]which doesn't exist. Try looping through to probKumulatif.Length - 1
您得到的索引越界,因为i + 1当您循环遍历每个项目时,您指的是循环内的索引。因此,您将尝试访问probKumulatif[probKumulatif.Length]不存在的最后一次迭代。尝试循环到probKumulatif.Length - 1
// *** On this line ***
for (int i = 0; i < probKumulatif.Length - 1; i++)
{
//this is where the Index was outside the bounds of the array appear
if (probKumulatif[i] <= rnd && probKumulatif[i + 1] > rnd )
{
pilih = resultRil[i + 1];
}
else if ( rnd <= probKumulatif[0])
{
pilih = resultRil[0];
}
}
You're also referring to resultRilusing iand not n. If you mean nthen the same can be applied as above as you access resultRil[i + 1]
您还指的是resultRil使用i而不是n. 如果您的意思是,n那么在您访问时可以应用与上述相同的内容resultRil[i + 1]
for (int n = 0; n < resultRil.Length - 1; n++)
{
...
}
You may need to refer to resultRilusing nin the inner loop
您可能需要参考resultRil使用n在内环
pilih = resultRil[n + 1];
回答by Blorgbeard is out
for (int i = 0; i < probKumulatif.Length; i++)
{
//this is where the Index was outside the bounds of the array appear
if (probKumulatif[i] <= rnd && probKumulatif[i + 1] > rnd )
What happens on the last iteration of this loop, when i == probKumulatif.Length - 1?
这个循环的最后一次迭代会发生什么,什么时候i == probKumulatif.Length - 1?
If probKumulatif[i] <= rndis true, then probKumulatif[i + 1] > rndwill be evaluated, and since i == probKumulatif.Length - 1, then i + 1 == probKumulatif.Length- so you try to access probKumulatif[probKumulatif.Length], which causes your exception!
如果probKumulatif[i] <= rnd是真的,那么probKumulatif[i + 1] > rnd将被评估,并且因为i == probKumulatif.Length - 1,那么i + 1 == probKumulatif.Length- 所以你尝试访问probKumulatif[probKumulatif.Length],这会导致你的异常!
Note that since rndis random, this will only happen sometimes, with greater probability the more times you run it!
请注意,由于rnd是random,这只会偶尔发生,您运行它的次数越多,概率就越大!
回答by beebee
simple. because at the last run of the loop probKumulatif[i + 1] tries to reach the probKumulatif[probKumulatif.Length] which is not allowed. the max index you can reach is probKumulatif[probKumulatif.Length-1]. please try to debug your code before posting.
简单的。因为在循环的最后一次运行时, probKumulatif[i + 1] 试图达到不允许的 probKumulatif[probKumulatif.Length]。您可以达到的最大索引是 probKumulatif[probKumulatif.Length-1]。请在发布前尝试调试您的代码。
回答by wy__
The error occurs because you tried to access array at the index that out of its range. You can simply check as following:
发生错误是因为您试图访问超出范围的索引处的数组。您可以简单地检查如下:
if (probKumulatif[i] <= rnd && (probKumulatif.Length >= i+1 && probKumulatif[i + 1] > rnd) )
{
pilih = resultRil[i + 1];
}
else if ( rnd <= probKumulatif[0])
{
pilih = resultRil[0];
}
but I would suggest for chage the for loop insteads:
但我建议改为使用 for 循环:
for (int i = 0; i < probKumulatif.Length-1; i++)
{
//Keep it the same.
}

