【问题标题】:ArrayIndexOutOfBoundsException, I get this strange error, even when I loop the array keeping in mind the indexArrayIndexOutOfBoundsException,我得到这个奇怪的错误,即使我循环数组时记住索引
【发布时间】:2015-03-31 12:59:12
【问题描述】:

我知道数组从索引零开始,当我调用数组时,我需要牢记这一点。

例如:

public int name [] = name [3]
for (int i = 0 ; i < 3 ; i++){
    name [i] = 0 ;
}

当我为我的模型编写代码时,我会以同样的方式进行。但是,我反复收到错误java.lang.ArrayIndexOutOfBoundsException

这是我的代码:

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 中得到reward_problem[][] 数组的错误java.lang.ArrayIndexOutOfBoundsException

而且,我的大部分代码都不断收到此错误。

我收到mem_payoff_time_action[][] 的相同错误。我在写:

for (t = 0; t< T; t++){
    for (a = 0; a < A ; a++){
        mem_payoff_time_action[t][a] = 0;
    }
}

为了修复错误,我将“A”更改为action_mean.length。而且,我创建了一个名为 Time 的数组。

public int Time [] = new int [T];
public int calcualte_time (){
    for (int t : Time){
        Time [t] = 0;
    }
    return Time [t];
}

而且,我重写了代码,如下所述。而且,一切都很好。但是,我仍然不明白为什么会出现此错误。

【问题讨论】:

  • 一些标识会很好。
  • 另外请不要使用大写字母作为变量名...
  • 我不明白你的建议或要求..
  • @user3469181,你能发布你得到的确切错误吗?以及你得到它的确切代码行?
  • 线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException: 100 at faces.User.calculate_reward_cummulative(User.java:617) at faces.User.main(User.java:1403)

标签: java eclipse multidimensional-array


【解决方案1】:

正如您正确指出的那样,您得到的错误是在 calculate_reward_cummulative 方法中,原因如下:

返回reward_problem[t][a];

该数组在逻辑上将有 10 行(索引 0-9)和 100 列(索引 0-99)。如果你试图获取第 100 个索引的值,你一定会得到这个异常。

这里 t 是 0,但 a 是一个全局变量,你在 for 循环中将它用作计数器,

for (a= 0 ; a

当循环结束时计数器的值即“a”为100,因此您试图返回reward_problem[0][100],因此jvm正在抛出异常。

请尝试使用不同的计数器变量并返回适当的数组索引。

【讨论】:

  • 我按照你的建议做了,但我仍然得到同样的错误。
猜你喜欢
  • 1970-01-01
  • 2011-11-09
  • 2012-07-09
  • 2019-02-23
  • 2015-09-20
  • 2016-07-03
  • 2014-12-03
  • 2016-03-01
  • 1970-01-01
相关资源
最近更新 更多