ArrayIndexOutOfBoundsException,即使我在牢记索引的情况下循环数组,也会收到此奇怪的错误 [英] ArrayIndexOutOfBoundsException, I get this strange error, even when I loop the array keeping in mind the index
问题描述
我知道数组从索引0开始,当我调用数组时,我需要牢记这一点.
I know that arrays start with index zero and, when I call an array, I need to keep this in mind.
例如:
public int name [] = name [3]
for (int i = 0 ; i < 3 ; i++){
name [i] = 0 ;
}
当我为模型编写代码时,我以相同的方式进行操作.但是,我反复收到错误 java.lang.ArrayIndexOutOfBoundsException
.
And I proceed in the same way when I write the code for my model. However, I get the error java.lang.ArrayIndexOutOfBoundsException
repeatedly.
这是我的代码:
public int T = 10;
public int A = 100;
public int t = 0;
public int a = 0
public double d = 2.5;
public double weight = 0;
public double vision; // I initialize this later and it goes smoothly.
public double mem_payoff_time_action[][] = new double [T][A];
public double reward_problem[][] = new double [T][A];
public int action_taken[][] = new int [T][A];
public double action_mean []= new double [A];
public double getaction_mean() {
for ( int p = 0; p < action_mean.length ;p++){
action_mean[p] = (p + 1);
}
return action_mean[a];
}
public double calculate_mem_payoff_matrix_initialize (){
for ( int t: Time ){
for (a = 0 ; a == (action_mean.length - 1) ; a++){
mem_payoff_time_action[t][a] = 0;
}
}
return mem_payoff_time_action[t][a];
}
public double attach_this_weight(){
for (a = 0 ; a < A ;a++){
if (action_taken[t][a] == 1){
if (vision == 1){
if (mem_payoff_time_action[t][a] > 0.0 ){
weight = 0.1 ;
}
else{
weight = - (0.1);
}
}
if (vision == 2){
if (mem_payoff_time_action[t][a] > 0.0 ){
weight = 0.2 ;
}
else{
weight = - (0.2);
}
}
if (vision == 3){
if (mem_payoff_time_action[t][a] > 0.0 ){
weight = 0.3 ;
}
else{
weight = - (0.3);
}
}
}
}
return weight;
}
public double calculate_reward_cummulative(){
int p= 0;
if (t ==0){
for ( a= 0 ; a < action_mean.length ; a++){
if (a == first_action_index){
if (mem_payoff_time_action[0][a] < 0){
reward_problem[0][a] = d + (weight* mem_payoff_time_action[0][a]);
else{
reward_problem[0][a] = d + (weight* mem_payoff_time_action[0][a]);
}
}
else{
reward_problem[0][p] = d;
}
}
}
else{
for( p =0; p < action_mean.length ;p++){
if(action_taken[t][p]== 1){
if(mem_payoff_time_action[t][p] < 0){
reward_problem[t][p] = (reward_problem[t-1][p]) + (weight* mem_payoff_time_action[t][p]) ;
}
else{
reward_problem[t][p] = (reward_problem[t-1][p]) + (weight* mem_payoff_time_action[t][p]) ;
}
}
else{
reward_problem[t][p] = reward_problem[t-1][p];
}
}
}
return reward_problem[t][a];
}
我在方法 calculate_reward_cummulative
而且,我的大多数代码都不断收到此错误.
And, I keep getting this error for most of my code.
我在 mem_payoff_time_action [] []
上收到了相同的错误.我在写:
I was receiving the same error formem_payoff_time_action[][]
. I was writing:
for (t = 0; t< T; t++){
for (a = 0; a < A ; a++){
mem_payoff_time_action[t][a] = 0;
}
}
为了解决该错误,我用 action_mean.length
更改了'A'.而且,我创建了一个名为时间的数组.
In order to fix the error, I change 'A' with action_mean.length
. And, I created an array called Time.
public int Time [] = new int [T];
public int calcualte_time (){
for (int t : Time){
Time [t] = 0;
}
return Time [t];
}
而且,我重写了下面提到的代码.而且,一切都很好.但是,我仍然不明白为什么会出现此错误.
And, I rewrote the code as mentioned below. And, everything was fine. However, I still don't understand why I get this error.
推荐答案
正如您正确指出的那样,您得到的错误是在calculate_reward_cummulative方法中,原因如下:
as you rightly pointed out the error you are getting is in calculate_reward_cummulative method, the reason being the following:
返回reward_problem [t] [a];
return reward_problem[t][a];
该数组在逻辑上将具有10行(索引0-9)和100列(索引0-99).如果您尝试获取第100个索引的值,则肯定会遇到此异常.
the array will logically have 10 rows (index 0-9) and 100 columns (index 0-99). if you try to get the value for the 100th index, you are bound to get this exception.
这里t变为0,但是a作为全局变量,您将其用作for循环中的计数器,
here t is coming as 0 but as a is a global variable and you are using it as a counter in the for loop,
for(a = 0; a< action_mean.length; a ++)
for ( a= 0 ; a < action_mean.length ; a++)
当循环结束时,计数器的值(即"a")为100,因此您试图返回reward_problem [0] [100],因此jvm抛出异常.
when the loop ends the value of the counter ie "a" is 100, thus you are trying to return reward_problem[0][100], thus jvm is throwing exception.
请尝试使用其他计数器变量并返回适当的数组索引.
please try to use a different counter variable and return the appropriate array index.
这篇关于ArrayIndexOutOfBoundsException,即使我在牢记索引的情况下循环数组,也会收到此奇怪的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!