错误:索引超出了数组的范围 [英] Error: Index was outside the bounds of the array

查看:88
本文介绍了错误:索引超出了数组的范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正面临这个问题索引超出了数组的界限.
即使我增加了数组的大小,这个问题仍然存在.

Iam facing this problem Index outside the bounds of array.
Even if i increase the size of array, this problem still persist.

public partial class corelation_test : Form
   {
       public corelation_test()
       {
           InitializeComponent();
       }
       int[] x_prev = new int[2000000];
       int[] y_prev = new int[2000000];

       public corelation_test(int [] x,int [] y)
       {
           InitializeComponent();

           x.CopyTo(x_prev,0);
           y.CopyTo(y_prev,0);

       }
              int count = 0;      
              double[] min_x = new double[20000000];
              double[] min_y = new double[20000000];
                int k = 0;
               double [] min = new double [20000000];
do
 {
  for (double i = (x_prev[count] - 10.5); i<(x_prev[count] + 10.5); i++)
  {
  for (double j = (y_prev[count] - 10.5); j<(y_prev[count] + 10.5); j++)
  {
  min[k]=Math.Pow (((x_prev [count]+11)-(x_prev [count ]+i+21))+((y_prev [count ]+11)-(y_prev [count ]+j+21)),2);        
k++;
   }
   }

Error is in array min[k].
While project gets build it stops at line :
  min[k]=Math.Pow (((x_prev [count]+11)-(x_prev [count ]+i+21))+((y_prev [count ]+11)-(y_prev [count ]+j+21)),2);

推荐答案

在该变量,或者当给出错误并使用鼠标在其上遍历时,您也应该能够看到它.该值可能超出数组范围,因此您应该再看看如何确定该值.

顺便说一句,您有一个嵌套的for循环,其产生的数量可能比您预期的要多得多.确定生成多少数据,而不用猜测固定大小.另一种选择是使用可以动态增长的容器类.

祝你好运!
Have a look at k by adding watch on that variable or you should also be able to see it when the error is given and you go over it with the mouse. The value is probably out of range of the array so you should have another look at how that value is determined.

By the way.You have a nested for loop that probably is producing a lot more than you expect. Determine how much data is generated instead of guessing a fixed size. Another option would be to use a container class that can grow dynamically.

Good luck!


如果没有通过调试器实际运行它,很难告诉你问题出在哪里-我无法通过调试器运行它,因为它无法编译:您的do循环不会以while终止,并且仍然在所有方法之外.

用几行替换.
为每个数组访问创建临时值,并将它们放在单独的行中.
然后,当发生错误时,您可以判断出哪个数组访问引起了问题,并可以查看该值以及它如何变得如此之大或为负.
Without actually running this through the debugger it is difficult to tell you what the problem is - and I can''t run it throughthe debugger because it won''t compile: your do loop is not terminated with a while and is outside all methods anyway.

Replace your line with several.
Create temporary values for each array access and put them on separate lines.
Then when the error occurs , you can tell which array access is giving the problem, and can look at the value, and how it got to be that large or negative.


k的值变得大于或等于20000000.您可以设置一个条件,请参见此内容.

for(double i =(x_prev [count]-10.5); i<(x_prev [count] + 10.5); i ++)
{
for(double j =(y_prev [count]-10.5); j<(y_prev [count] + 10.5); j ++)
{
min [k] = Math.Pow((((x_prev [count] +11)-(x_prev [count] + i + 21))+((y_prev [count] +11)-(y_prev [count] + j + 21 )),2);
k ++;
if(k> = 20000000)
休息;
}
if(k> = 20000000)
休息;
}

这样,您的程序将不会崩溃,但您的需求将无法完全满足.
您还应该提供x_prev [count]的值和完全do while循环.然后我可以低估为什么k的值会增加这么多.
The value of k became large than or equal 20000000. You can set a condition so see this.

for (double i = (x_prev[count] - 10.5); i<(x_prev[count] + 10.5); i++)
{
for (double j = (y_prev[count] - 10.5); j<(y_prev[count] + 10.5); j++)
{
min[k]=Math.Pow (((x_prev [count]+11)-(x_prev [count ]+i+21))+((y_prev [count ]+11)-(y_prev [count ]+j+21)),2);
k++;
if(k>=20000000)
break;
}
if(k>=20000000)
break;
}

by doing this, your program will not crash but your demands will not full fill.
You also should give the value of x_prev[count] & full do while loop. Then i can understard why the value of k is increasing so much.


这篇关于错误:索引超出了数组的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆