Matrix multiplication in Java

2

I need to make two arrays multiply, but I'm wrong at the time of multiplication.

    int ma[][] = new int [3][2];
    int mb[][] = new int [2][2];
    int mab[][] = new int [3][2];

    for (int i = 0; i < ma.length; i++) {
        for (int j = 0; j < ma[i].length; j++) {
            System.out.println("Digite o elemento da linha " + (i + 1) + " e coluna " + (j + 1) + " da matriz 1");
            ma[i][j] = new Scanner(System.in).nextInt();
        }
    }

    for (int i = 0; i < mb.length; i++) {
        for (int j = 0; j < mb[i].length; j++) {
            System.out.println("Digite o elemento da linha " + (i + 1) + " e coluna " + (j + 1) + " da matriz 2");
            mb[i][j] = new Scanner(System.in).nextInt();
        }
    }

    mab[1][1] = (ma[1][1] * mb[1][1]) + (ma[1][2] * mb[2][1]);
    mab[1][2] = (ma[1][1] * mb[1][2]) + (ma[1][2] * mb[2][2]);
    mab[2][1] = (ma[2][1] * mb[1][1]) + (ma[2][2] * mb[1][2]);
    mab[2][2] = (ma[2][1] * mb[1][2]) + (ma[2][2] * mb[2][2]);
    mab[3][1] = (ma[3][1] * mb[1][1]) + (ma[3][2] * mb[2][1]);
    mab[3][2] = (ma[3][1] * mb[1][2]) + (ma[3][2] * mb[2][2]);

    System.out.println("Multipliacação das matrizes:");

    for (int i = 0; i < mab.length; i++) {
        for (int j = 0; j < mab[i].length; j++) {
            System.out.println(mab[i][j] + "\t");
        }
    }

In netbeans, it indicates that the error message on line 26, which is where the first multiplication occurs,

  

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2       at multiplicacao.Multiplicacao.main (Multiplicacao.java:26)

Did I miss the deal? I'm new to java

    
asked by anonymous 26.10.2017 / 12:47

2 answers

5

When you create a vector, you pass the size of it new int[tamanho] as a parameter. Vector indexes start at 0, as per Oracle documentation . You can also view this link from caelum over vectors.

If you create a int vetor = new int[2] vector then it has the vetor[0] and vetor[1] positions.

In your case, when you created the array int mb[][] = new int [2][2]; there is no index mb[X][2] indexes exist from mb[0][0] to mb[1][1]

You can make the following adjustment in the code:

//Seu código
mab[1][1] = (ma[1][1] * mb[1][1]) + (ma[1][2] * mb[2][1]);
//Ajuste
mab[0][0] = (ma[0][0] * mb[0][0]) + (ma[0][1] * mb[1][0]);
    
26.10.2017 / 13:36
5
  

java.lang.ArrayIndexOutOfBoundsException

It means that you are accessing a position outside the valid array positions.

In your case this happens here:

mab[3][2] = (ma[3][1] * mb[1][2]) + (ma[3][2] * mb[2][2]);

Array indices in Java always start at 0 , then ma that was set to:

int ma[][] = new int [3][2];

It has valid positions for [0 a 2][0 a 1] .

However the logic you are doing should be done through a loop / cycle, so that it can be dynamic and adaptable to any dimensions.

Here is an example of a calculation with for and matching other details that were less well:

Scanner teclado = new Scanner(System.in); //criar o Scanner apenas uma vez

int ma[][] = new int[3][2];
int mb[][] = new int[2][2];
int mab[][] = new int[3][2];

for (int i = 0; i < ma.length; i++) {
    for (int j = 0; j < ma[i].length; j++) {
        System.out.println("Digite o elemento da linha " + (i + 1) + " e coluna " + (j + 1) + " da matriz 1");
        ma[i][j] = teclado.nextInt(); //ler com base no Scanner criado
    }
}

for (int i = 0; i < mb.length; i++) {
    for (int j = 0; j < mb[i].length; j++) {
        System.out.println("Digite o elemento da linha " + (i + 1) + " e coluna " + (j + 1) + " da matriz 2");
        mb[i][j] = teclado.nextInt(); //ler com base no Scanner criado
    }
}

//calculo da multiplicação das matrizes
for(int i=0; i<mab.length; i++){
    for(int j=0; j<mab[i].length; j++){
        for(int k=0; k<ma[i].length; k++){
            mab[i][j] += ma[i][k] * mb[k][j];
        }
    }
}

Ideone Example

Read also another answer I've already given about multiplication of arrays in C

    
26.10.2017 / 13:38