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();
}
}
}