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] + ", ");
        }
    }
}

So, I am trying to pass an array P to floyd2 and it keeps giving me an arrayOutOfBoundsExeception and it does not like the first for loop in floyd2. What could possibly be giving me an array out of bounds?!

If I remove the array P the code works on its own.

EDIT: Stack trace --

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
xenteros
  • 15,586
  • 12
  • 56
  • 91
Felicia
  • 33
  • 1
  • 1
  • 8

4 Answers4

4

The issue was due to your code trying to access index that doesn't exist. Please replace your code ,

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

with

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

You are increasing i instead of j on your for loop statement thus causing ArrayIndexOutOfBoundsException.

Runcorn
  • 5,144
  • 5
  • 34
  • 52
2
     for(i=0; i < n; i++){ //This does not work
    for(j=0; j < n; i++){
        P[i][j] = 0;
    }
  }

in your second loop you have i++ instead of j++

Amer Qarabsa
  • 6,412
  • 3
  • 20
  • 43
1

The stacktrace says:

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)

It's the loop:

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

In this loops, you increment i twice. So it becomes n which is 5. Change to j++ in the inner loop.

xenteros
  • 15,586
  • 12
  • 56
  • 91
1
for(i=0; i < n; i++){ //This does not work
    for(j=0; j < n; i++){
        P[i][j] = 0;
    }
}

needs to be

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

make sure you increment the same variable you are testing in the for loop (j in this case)

joe pelletier
  • 390
  • 2
  • 12