Question

On the line with the "here" comment I get an index out of bounds error for some input data. It does not always occur. It does not occur when the program is run on my machine, even. It occurs when a project teammate runs the project on his laptop.

I know there's output from the exception regarding the actual index, but for reasons I don't have that information right now. I might be able to update with the full exception soon.

public int getDistance(int[] arrayA, int[] arrayB) {
    int[][] array = new int[arrayA.length][arrayB.length];

    for (int i = 0; i < arrayA.length; i++)
        array[i][0] = i;

    for (int i = 0; i < arrayB.length; i++)
        array[0][i] = i; // Here

I cut the method off here as the remainder shouldn't be relevant.

As you can see I create a two dimensional array from two single dimensional arrays (arrayA and arrayB) and the width and height of that array equals the length of the two single dimensional arrays. Then I set the "leftmost" column to incrementing numbers and the "uppermost" row to incrementing numnbers.

If you need more code, I can post it.

Was it helpful?

Solution

When at least one of the array's lenght is 0 then it throws ArrayIndexOutOfBoundsException. Try for example:

int[] arrayA = new int[4];
int[] arrayB = new int[0];

getDistance(arrayA, arrayB);

OTHER TIPS

my guess is that the exception occurs when one of the lists is of length 0, ie. lets say arrayA is the problem and has length=0, you then create array [0][arrayB.length] and when you then try to call array[0][i] it cant access element 0 and throws an OutofBoundsException

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top