2016-10-04 143 views
0
public static void main (String[] args){ 
    double infinity = Double.POSITIVE_INFINITY; 
    int num = 5; 
    double[][] W = {{0,1,infinity,1,5},{9,0,3,2,infinity},{infinity,infinity,0,4,infinity},{infinity,infinity,2,0,3},{3, infinity, infinity, infinity,0}}; //Weighted, directed graph 
    double[][] D = W; 
    double[][] P = new double[5][5]; 

    for(int i=0; i < num; i++){ //This works, but it throws the exception in the middle of this 
     System.out.println(""); 
     for(int j=0; j < num; j++){ 
      System.out.print("P["+i+"]"+"["+j+"]: "+ (int)P[i][j] + ", "); 
     } 
    } 

    floyd2(num, W, D, P); 

} 

private static void floyd2 (int n, double W[][], double D[][], double P[][]){ 
    int i, j, k; 

    for(i=0; i < n; i++){ //This does not work 
     for(j=0; j < n; i++){ 
      P[i][j] = 0; 
     } 
    } 

    D = W; 
    for(k=0; k< n; k++){ 
     for(i=0; i < n; i++){ 
      for(j=0; j < n; j++){ 
       if((D[i][k] + D[k][j]) < D[i][j]){ 
        P[i][j] = k; 
        D[i][j] = D[i][k] + D[k][j]; 
       } 
      } 
     } 
    } 
    //Output D 
    for(i=0; i < n; i++){ 
     for(j=0; j < n; j++){ 
      System.out.print("D["+i+"]"+"["+j+"]: "+ (int)D[i][j] + ", "); 
     } 
    } 
    //Output P 
    for(i=0; i < n; i++){ 
     for(j=0; j < n; j++){ 
      System.out.print("P["+i+"]"+"["+j+"]: "+ (int)P[i][j] + ", "); 
     } 
    } 
} 

所以,我想傳遞一個數組p來floyd2它不斷給我一個arrayOutOfBoundsExeception,它並不像第一for floyd2中的循環。什麼可能會給我一個數組越界?!傳遞一個陣列 - 在線程異常「主要」 java.lang.ArrayIndexOutOfBoundsException:5

如果我刪除數組P,代碼將自行運行。

編輯: 堆棧跟蹤 -

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 
at com.company.Main.floyd2(Main.java:32) 
at com.company.Main.main(Main.java:23) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:498) 
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) 
Process finished with exit code 1 
+0

粘貼堆棧跟蹤的問題相同的變量。 [mcve] – xenteros

+1

使用'''爲(j = 0; j Runcorn

+1

您試圖使用不存在的索引。您的調試器是找到這種情況發生的最快途徑。 –

回答

4

的問題是由於你的代碼試圖訪問不存在的索引。請

for(j=0; j < n; j++){ 
    P[i][j] = 0; 
} 

你對你對因此造成ArrayIndexOutOfBoundsException循環語句增加,而不是j的更換你的代碼,

for(j=0; j < n; i++){ 
    P[i][j] = 0; 
} 

2
 for(i=0; i < n; i++){ //This does not work 
    for(j=0; j < n; i++){ 
     P[i][j] = 0; 
    } 
    } 
在你的第二個循環

你有我++而不是J ++

1

堆棧跟蹤說:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 
    at com.company.Main.floyd2(Main.java:32) 
    at com.company.Main.main(Main.java:23) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) 

它的循環:

for(i=0; i < n; i++){ //This does not work 
     for(j=0; j < n; i++){ 
      P[i][j] = 0; 
     } 
    } 

在這種循環,可以增加i兩次。所以它變成n這就是5。在內部循環中更改爲j++

1
for(i=0; i < n; i++){ //This does not work 
    for(j=0; j < n; i++){ 
     P[i][j] = 0; 
    } 
} 

必須

for(i=0; i < n; i++){ 
    for(j=0; j < n; j++){ 
     P[i][j] = 0; 
    } 
} 

確保你增加你的(在這種情況下j)測試for循環

相關問題