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;
}
}
mdarray = a;?maxVal.lengthis2, not4. It only has indices0and1, hence theArrayIndexOutOfBoundsExceptionwhen you try to assignmdarray[i]which has the size of4. If you're working with rectangular arrays, you can changedouble[] maxVal = new double[mdarray.length]todouble[] maxVal = new double[mdarray[0].length]. If not, then you'll need another solution.