获取异常ArrayOutOfBounds

时间:2016-11-17 05:17:01

标签: java arrays java-threads

我是java程序的新手。在执行与线程相关的程序时,我得到了一个异常ArrayOutOfBounds。我犯的错误是什么。

请查看我的计划并尽快回复。我坚持这个程序。

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

3 个答案:

答案 0 :(得分:1)

从代码中删除k&lt; =

instead write

k< B.length

答案 1 :(得分:0)

我在猜测(因为您没有发布堆栈跟踪),您需要更改

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

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

答案 2 :(得分:0)

因为

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

k == B.length你仍然进入循环时,现在你想要将元素放在位置k。但是在容量为x的数组中,最后一个元素位于x-1位置,因为该数组是从0开始的,这意味着第一个元素是索引0,最后一个元素是索引array.length - 1。因此,您在B[k]时调用k == B.length,索引超出范围。

您可以将其更改为

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

或者,

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