-1

I'm new to java program. While executing a program related to threads I got an exception ArrayOutOfBounds. Whats the mistake I made.

Please review my program and reply back soon. I'm stuck with this program.

import java.util.Scanner

public class MatrixMulti implements Runnable {

    private int row;
    private int col;
    private int A[][];
    private int B[][];
    private int C[][];

    public MatrixMulti(int row, int col,int A[][], int B[][], int C[][] ) {
        this.row = row;
        this.col = col;
        this.A = A;
        this.B = B;
        this.C = C;
    }

    public void run() {      
        for(int k = 0; k <=B.length; k++) {
            C[row][col] += A[row][k] * B[k][col];                  
        }

    } 


    public static void main(String args[]) {
        int row;
        int col;

        System.out.println("Enter the base of squared matrices");
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();

        int[][] A=new int[n][n];
        int[][] B=new int[n][n];
        int[][] C=new int[n][n];

        try{

            System.out.println("Enter the elements of 1st martix row wise \n");
            for(row = 0 ; row < n; row++) {
                for (col = 0 ; col < n; col++ ) {                
                    A[row][col] = input.nextInt();
                }
            }

            System.out.println("Enter the elements of 2nd martix row wise \n");

            for(row = 0 ; row < n; row++) {
                for (col = 0 ; col < n; col++ ) {
                    B[row][col] = input.nextInt();                   
                }
            }         

            System.out.println(" Matrix A: ");

            for(row = 0 ; row < n; row++) {
                for (col = 0 ; col < n; col++ ) {
                    System.out.print("  "+A[row][col]);
                }
                System.out.println();
            }


            System.out.println(" Matrix B: ");

            for(row = 0 ; row < n; row++) {
                for (col = 0 ; col < n; col++ ) {
                    System.out.print("  "+B[row][col]);
                }
                System.out.println();
            }

            int threadcount = 0;             

            Thread[] thrd = new Thread[n*n];

            for(row = 0 ; row < n; row++){
                for (col = 0 ; col < n; col++ ) {                                   
                    thrd[threadcount] = new Thread(new MatrixMulti(row,col,A, B, C));
                    thrd[threadcount].start(); 
                    thrd[threadcount].join(); 
                    threadcount++;
                }

            }

        } catch (InterruptedException ie){}


        System.out.println(" Product Matrix C: ");
        for(int i=0; i<n; i++){
            for(int j=0; j<n; j++){
                System.out.print("  "+C[i][j]);
            }
            System.out.println();               
        }
    }
}
Lahiru Ashan
  • 602
  • 9
  • 16
Sukanth K
  • 11
  • 6

3 Answers3

1

remove k<= from code

instead write

k< B.length
Akshay
  • 817
  • 7
  • 16
0

I am guessing (as you did not post the stacktrace) you need to change

for(int k = 0; k <=B.length; k++)

to

for(int k = 0; k < B.length; k++)
Scary Wombat
  • 41,782
  • 5
  • 32
  • 62
  • This is what it looked like to me too, however it also helps the user if you explain why they should change it too. – Sean F Nov 17 '16 at 05:22
0

Because by

for (int k=0; k<=B.length; k++)

when k == B.length you still get into the loop, and now you want to get the element at position k. But in an array of a capacity of x, the last element is at position x-1, because the array is 0-based, meaning that the first element is of index 0, and the last, of index array.length - 1. So you by calling B[k] when k == B.length, the index is out of range.

You can change it to

 for (int k=0; k<B.length; k++)

Or,

 for (int k=0; k<=B.length - 1; k++)
WesternGun
  • 7,222
  • 56
  • 97