2014-02-10 30 views
-2

我不知道爲什麼我在嵌套for循環中得到了ArrayIndexOutOfBoundsException ...它只打印第一個內部for循環,並且從不進行循環外部循環。
誰能告訴我爲什麼會發生這種情況?嵌套for循環中的ArrayIndexOutOfBoundsException?

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; 
} 
+0

你的數組大小是多少? – rcs

+0

「exData」和「exLabels」數組的內容和大小是多少?它們是否在'i'和'j'所達到的值的範圍內。請指定您發現異常的行。 – melc

+0

完整的錯誤消息應該告訴你導致問題的行和索引。這與數組的大小相結合,應該告訴你爲什麼你會得到一個異常,並且可能還會如何解決它。 – Dukeling

回答

0

最可能的原因是 '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; 
}