I'm writing a program for class where we hard code matrices in a driver class and put the matrix operations in a matrix class. I'm running into an issue where I get the error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
I'm pretty sure that it's the second for loop but I can't think of what range to do to refer to the columns of the hard coded matrix in the driver class.
row
and col
are both instantiated in the Matrix
class and I fixed the j++
but it still gives the same error even when I'm doing a 1x2
multiplied by a 2x3
, for example
public Matrix mult(Matrix m) {
// TODO: Multiply the two matrices, store the value
// in a new matrix and return that matrix
Matrix m5 = new Matrix(new int[row][col]);
for (int i = 0; i < row; i++) {
for (int j = 0; j < m.myMatrix[0].length; j++) {
for (int k = 0; k < col; k++) {
m5.myMatrix[i][j] += myMatrix[i][k] * m.myMatrix[k][j];
}
}
}
return m5;
}