错误:指​​数数组的边界之外。 [英] Error : Index was outside the bounds of the array.

查看:218
本文介绍了错误:指​​数数组的边界之外。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道是什么问题,说明的,但我很困惑我的程序是如何输出值,这就是阵列之外。

我有整数是0的数组 - 8,这意味着它可以容纳9整数,正确吗?
我有检查,以确保用户的输入值是1-9的int。我删除一个来自整数(像这样)

 如果(posStatus [intUsersInput-1] == 0)//如果pos空
{
    posStatus [intUsersInput-1] + = 1;
} //将它设置为1

然后我输入9我自己和我得到的错误。它应该进入最后的int数组,所以我不明白为什么我得到一个错误。
有关code:

 公众诠释[] posStatus;公共UsersInput()
{
    this.posStatus =新INT [8];
}INT intUsersInput = 0; //这个被尝试解析+验证了它的1-9如果(posStatus [intUsersInput-1] == 0)//如果我输入9就应该到8?
{
    posStatus [intUsersInput-1] + = 1; //设置为1
}

错误:

 索引数组的范围之外。 指数数组的边界之外。


解决方案

您已经宣称,可以存储8个元素的数组不是9。

  this.posStatus = INT新[8];

这意味着postStatus将包含索引0 8个元素为7。

I'm aware of what the issue is stating but I am confused to how my program is outputting a value that's outside of the array..

I have an array of ints which is 0 - 8 which means it can hold 9 ints, correct? I have an int which is checked to make sure the users input value is 1-9. I remove one from the integer (like so)

if (posStatus[intUsersInput-1] == 0) //if pos is empty
{
    posStatus[intUsersInput-1] += 1; 
}//set it to 1

then I input 9 myself and I get the error. It should access the last int in the array, so I don't see why I'm getting an error. Relevant code:

public int[] posStatus;       

public UsersInput()    
{    
    this.posStatus = new int[8];    
}

int intUsersInput = 0; //this gets try parsed + validated that it's 1-9    

if (posStatus[intUsersInput-1] == 0) //if i input 9 it should go to 8?    
{    
    posStatus[intUsersInput-1] += 1; //set it to 1    
} 

Error:

"Index was outside the bounds of the array." "Index was outside the bounds of the array."

解决方案

You have declared an array that can store 8 elements not 9.

this.posStatus = new int[8]; 

It means postStatus will contain 8 elements from index 0 to 7.

这篇关于错误:指​​数数组的边界之外。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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