传递数组 - 线程中的异常" main" java.lang.ArrayIndexOutOfBoundsException:5

时间:2016-10-04 19:04:17

标签: java arrays exception

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,它不喜欢floyd2中的第一个for循环。什么可能给我一个数组出界?!

如果删除数组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

4 个答案:

答案 0 :(得分:4)

问题是由于您的代码尝试访问不存在的索引。请替换您的代码,

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

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

你在for循环语句中增加 i 而不是j,从而导致ArrayIndexOutOfBoundsException

答案 1 :(得分:2)

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

在你的第二个循环中你有i ++而不是j ++

答案 2 :(得分:1)

stacktrace说:

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++

答案 3 :(得分: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;
     }
 }

确保在for循环中增加您正在测试的相同变量(在这种情况下为j