我收到的索引错误超出了C#中数组的范围 [英] I am getting an error of index was outside the bounds of the array in C#

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

问题描述

我在heuristiclab中执行下面的代码,我得到一个错误索引超出了数组的范围。我也检查过阵列的范围。我有一个矩阵,10个变量作为输入,有40行数据。



我尝试过:



public double Evaluate(个人,IRandom随机){

//使用vars.yourVariable访问变量商店中的变量,即yourVariable

var x =(DoubleMatrix)vars.x;

var w =(DoubleArray)vars.w;

var obj =(DoubleArray)vars.obj;

var quality = 0.0;

double sum = 0.0;

// quality = individual.RealVector(r)。Sum(x => x * x);

var solution = individual.BinaryVector(kp);



//随机随机=新的随机();



for(int i = 0; i< 10; i ++)

{

w [i] = 0.0;

(b = b; j <39; j ++)

{

for(int i = 0; i< 10; i ++)

{sum = sum +(w [i] * x [j,i]);

}

obj [j] = sum - x [j,10];

quality = quality + obj [j];

}







返回质量;

}

i am executing the code below in heuristiclab where i am getting an error Index was outside the bounds of the array. i have checked the range of an array also. i have a matrix with 10 variable as input and have data of 40 rows.

What I have tried:

public double Evaluate(Individual individual, IRandom random) {
// Use vars.yourVariable to access variables in the variable store i.e. yourVariable
var x = (DoubleMatrix)vars.x;
var w = (DoubleArray)vars.w;
var obj = (DoubleArray)vars.obj;
var quality = 0.0;
double sum = 0.0;
//quality = individual.RealVector("r").Sum(x => x * x);
var solution = individual.BinaryVector("kp");

//Random random = new Random();

for (int i=0; i<10;i++)
{
w[i] = 0.0;
}
for(int j=0; j<39; j++)
{
for(int i=0; i<10; i++)
{sum = sum + (w[i]* x[j,i]);
}
obj[j] = sum - x[j,10];
quality = quality + obj[j];
}



return quality;
}

推荐答案

使用调试器:在行<上设置断点br />
Use the debugger: Set a breakpoint on the line
for (int i=0; i<10;i++)



并运行你的应用程序。当它到达该行时,它将停止并让您控制。

您可以通过将鼠标悬停在名称上来查看变量,然后通过工具栏单步执行,以便执行单个一次指令。

仔细查看你的变量,看看你是否能发现问题出现的地方:在某个地方,你使用的i或j值对于数组来说太大了正试图索引。



我们不能为您做到这一点 - 我们无法访问您的变量商店,因此我们无法运行您的代码。



(而变量商店意味着全局变量 - 这是一个非常非常糟糕的主意。特别是当你开始学习基于OOP的语言时出于正当理由,它没有真正的全局变量。)


and run your app. When it reaches the line, it will stop and let you take control.
You can look at variables by hovering the mouse over the name, and single step the execution via the toolbar so that it executes a single instruction at a time.
Look at your variables closely, and see if you can spot where the problem is occurring: somewhere, you are using a value of i or j that is too big for the array you are trying to index.

We can't do that for you - we don't have access to your "variable store" so we can't run your code.

(And "variable store" implies a "global variable" - which is a very, very poor idea. Especially when you are starting to learn an OOPs based language that doesn't have truly global variables for good reasons.)


您应该学习尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。 />
当代码不做ex的时候你接近一个bug。



使用调试器来检查数组是否具有预期的大小。
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.

Use the debugger to check if arrays are of expected size.


你是得到此错误,因为当您尝试访问不可用的数组/列表的索引时,将抛出错误。

您必须在访问索引之前验证项目长度数组..

示例:

You are getting this error because, when you try to access an index of an Array/List which is not available, then the error will be thrown.
You have to validate the items length before accessing the index of the Array..
example:
for (int i = 0; i < 10; i++)
        {
            if (w.Length >= 10) // Validate the Length of the items in collectoin before accessing the index 
                w[i] = 0.0;
        }





在从数组访问索引的所有地方进行验证



Validate in all the places where the Index is being accessed from an Array


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

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