【问题标题】:ArrayIndexOutOfBoundsException in Nested for loop?嵌套for循环中的ArrayIndexOutOfBoundsException?
【发布时间】:2014-02-10 07:04:23
【问题描述】:

我不知道为什么我在嵌套的 for 循环内有一个 ArrayIndexOutOfBoundsException... 它只打印第一个内部 for 循环,而从不继续到外部 for 循环。
谁能告诉我为什么会这样?

public static double weight(){
    //int counter = 0;
    System.out.println("####" + d);
    while(Math.abs(delta_w) > 0.001){
        for(int i = 0; i < 33; i++ ){

            for(int j = 0; j < 13; j++ ){

                if(Math.abs(delta_w)!=0){

                    value = d.exData[i][j];    //?

                    y = d.exLabels[i];

                    percep = w * d.exData[i][j];
                    System.out.println(d.exData[i][j]);
                    delta_w = y - percep;

                    w+= delta_w;
//                  counter++;
                    System.out.println("value w " + w);
                }
            }
        }
    }

    return w;
}

【问题讨论】:

  • 你的数组大小是多少?
  • exDataexLabels 数组的内容和大小是多少?它们是否在ij 达到的值的范围内。请指定您获得异常的行。
  • 完整的错误消息应该告诉您导致问题的行和索引。这与数组的大小相结合,应该可以准确地告诉您为什么会出现异常,以及如何修复它。

标签: java indexoutofboundsexception


【解决方案1】:

最可能的原因是'd.exData.length == 1'

您为什么假设数组大小为 33 和 13?为什么不使用数组的计数呢?

public static double weight(){
    //int counter = 0;
    System.out.println("####" + d);
    while(Math.abs(delta_w) > 0.001){
        for(int i = 0; i < d.exData.length; i++ ){

            for(int j = 0; j < d.exData[i].length; j++ ){

                if(Math.abs(delta_w)!=0){

                    value = d.exData[i][j];    //?

                    y = d.exLabels[i];

                    percep = w * d.exData[i][j];
                    System.out.println(d.exData[i][j]);
                    delta_w = y - percep;

                    w+= delta_w;
                    //counter++;
                    System.out.println("value w " + w);
                }
            }
        }
    }

    return w;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-19
    • 2015-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-26
    相关资源
    最近更新 更多