【发布时间】: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