1

The goal of this assignment is to create a 2D array and then return the row with the maximum value in the array. When I try to call the method in the main method, I get the following:

java.lang.ArrayIndexOutOfBoundsException: 2

At this point, I'm not sure how to proceed.

public class MDArray
{
    private double[][] mdarray;

    public MDArray(double[][] a)
    {
        mdarray = new double[a.length][];
        for(int i = 0; i < a.length; i++)
        {
            mdarray[i] = new double[a[i].length];
            for(int j= 0; j < a[i].length; j++)
            {
                mdarray[i][j] = a[i][j];
            }
        }
    }
    public double[] max()
    {
        double[] maxVal = new double[mdarray.length];
        for(int i = 0, j = i + 1; i < maxVal.length; i++)
        {
            for(int k = 0; k < mdarray[i].length; k++)
            {
                if(mdarray[i][k] > mdarray[j][k])
                {
                    maxVal = mdarray[i];
                }
            }
        }
        return maxVal;
    }
}
6
  • What is the purpose of your constructor accepting an input 2D array if your code just overwrites it? Commented Dec 8, 2016 at 1:04
  • 1
    Is it really overwriting, or is he manually copying each and every value instead of of just mdarray = a;? Commented Dec 8, 2016 at 1:07
  • @Gendarme right you are Commented Dec 8, 2016 at 1:10
  • I have a test array with these values a = {{3, 4, 1, 8}, {13, 2, 12, 9}}. I'm trying to call it with a.max(). Commented Dec 8, 2016 at 1:12
  • 2
    This works if it's a matrix, but not if it's a 2x4 array like you have here. maxVal.length is 2, not 4. It only has indices 0 and 1, hence the ArrayIndexOutOfBoundsException when you try to assign mdarray[i] which has the size of 4. If you're working with rectangular arrays, you can change double[] maxVal = new double[mdarray.length] to double[] maxVal = new double[mdarray[0].length]. If not, then you'll need another solution. Commented Dec 8, 2016 at 1:13

1 Answer 1

1

If I understand what you are trying to do, I would start with a method to get the maximum value from a double[] like

private static double getMaxValue(double[] a) {
    int maxIndex = 0; // <-- start with the first
    for (int i = 1; i < a.length; i++) { // <-- start with the second
        if (a[i] > a[maxIndex]) {
            maxIndex = i;
        }
    }
    return a[maxIndex]; // <-- return the max value.
}

Then you can use that to determine the row with the maximum value (and copy the array) like

public double[] max() {
    int maxIndex = 0; // <-- start with the first
    for (int i = 1; i < mdarray.length; i++) { // <-- start with the second
        double maxValue = getMaxValue(mdarray[maxIndex]);
        double curValue = getMaxValue(mdarray[i]);
        if (curValue > maxValue) {
            maxIndex = i; // <-- The current value is greater, update the index.
        }
    }
    return Arrays.copyOf(mdarray[maxIndex], mdarray[maxIndex].length);
}

Finally, when constructing your MDArray you could also use Arrays.copyOf to simplify the logic like

public MDArray(double[][] a) {
    mdarray = new double[a.length][];
    for (int i = 0; i < a.length; i++) {
        mdarray[i] = Arrays.copyOf(a[i], a[i].length);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

@Gendarme I'm scratching my head too. I guess decomposing the problem was all the help OP needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.