-2
Good afternoon. I’m creating an algorithm for matrix multiplication, following the linear Algebra property. I chose to create a class with attributes: row and column, after obtaining these values by the constructor, create an array:
matrix = new int[row][column]
And then I created a method to add random values. Everything works. My question comes now... When I create the method to multiply the matrices, it looks like this:
public int[][] produtoMatrizes(CriarMatrizes m1, CriarMatrizes m2){
if (m1.getLinha() != m2.getColuna()) throw new RuntimeException("Dimensões inconsistentes. Impossível multiplicar as matrizes");
int[][] result = new int[m1.getLinha()][m2.getColuna()];
for (int i = 0; i < m1.getLinha(); i++)
for (int j = 0; j < m2.getColuna(); j++)
for (int k = 0; k < m1.getColuna(); k++)
result[i][j] += (m1[i][k] * m2[k][j]);
return result;
}
In this row: result[i][j] += (M1[i][k] * m2[k][j]); presents an error, which is: array required but Object found. I’d like some help on this part.
EDIT with the code of the Create matrices.java class
public class CriarMatrizes {
private int[][] matrix; int private line; int private column;
public CriarMatrizes(int linha, int coluna) {
this.linha = linha;
this.coluna = coluna;
matriz = new int[linha][coluna];
}
// os métodos gettes e setters
public void AdicionarValoresMatriz()
{
SecureRandom gerador = new SecureRandom();
for(int i = 0; i<getLinha(); i++)
{
for(int j = 0; j<getColuna();j++)
matriz[i][j] = gerador.nextInt(25);
}
}
public void ExibirMatriz(){
for(int i=0;i<getLinha();i++)
{
for(int j=0;j<getColuna();j++)
{
System.out.print(matriz[i][j] + " ");
}
System.out.print("\n");
}
}
public int[][] produtoMatrizes(CriarMatrizes m1, CriarMatrizes m2){
if (m1.getLinha() != m2.getColuna()) throw new RuntimeException("Dimensões inconsistentes. Impossível multiplicar as matrizes");
int[][] result = new int[m1.getLinha()][m2.getColuna()];
for (int i = 0; i < m1.getLinha(); i++)
for (int j = 0; j < m2.getColuna(); j++)
for (int k = 0; k < m1.getColuna(); k++)
result[i][j] += (m1[i][k] * m2[k][j]);
return result;
}
}
I put the code. There’s a simpler way to do this?
– vinicius peres
I put a solution with fewer changes in your code and also put a solution that I found simpler.
– Haruo
Thank you very much!!!!
– vinicius peres
If the answer solved your problem, please put the answer as useful and as accepted. Thank you.
– Haruo