异常数组超出范围

时间:2015-04-21 17:31:58

标签: java arrays matrix-multiplication vector-multiplication

我的代码需要将数组的每一行乘以下一列的每一列。用户输入高度和宽度,程序将随机生成数组值。如果数组相等,例如(3x3)(3x3)或即使你做(3x2)(2x3),也不会产生任何错误。如果你输入类似(3x3)(3x2)的东西,它会给出一个越界异常。

public class Multiply {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        int width = -1;
        int height = 0;
        int width2 = 0;
        int height2 = 0;

        while (width != height2) {

            while (height <= 0) {
                System.out.print("Enter a height for array1: ");
                height = checkInt(scan);
            }

            while (width <= 0) {
                System.out.print("Enter a width for array1: ");
                width = checkInt(scan);
            }

            while (height2 <= 0) {
                System.out.print("Enter a height for array2: ");
                height2 = checkInt(scan);
            }

            while (width2 <= 0) {
                System.out.print("Enter a width for array2: ");
                width2 = checkInt(scan);
            }

            if (width != height2) {
                System.out
                        .println("Error! Dimensions of matrices not compatible. Try again.");
                width = -1;
                height = 0;
                width2 = 0;
                height2 = 0;
            }
        }

        int array1[][] = randomMatrix(height, width);
        int array2[][] = randomMatrix(height2, width2);
        int product[][] = matrixMultiply(array1, array2, height, width2);

        printArray(product);

    }// end of main method

    public static int[][] randomMatrix(int height, int width) {
        int array1[][] = new int[height][width];
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                array1[i][j] = (int) (-10 + Math.random() * 20);
            }
        }
        return array1;
    }// end of randomMatrix method

    public static int[][] matrixMultiply(int[][] array1, int[][] array2, int height, int width) {
        int[][] product = new int[height][width];
        for (int i = 0; i < array1.length; i++) {
            for (int j = 0; j < array1[i].length; j++) {
                int prod = 0;
                for (int k = 0; k < array2.length; k++) {
                    prod = prod + array1[i][k] * array2[k][j];
                }
                product[i][j] = prod;
            }
        }
        return product;
    }// end of matrixMultiply method

    public static void printArray(int[][] array) {
        System.out.println(" ");
        System.out.println("Row Major Representation:");

        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                System.out.print(array[i][j] + " ");
            }
            System.out.println();
        }
        System.out.println(" ");
        System.out.println("Column Major Representation:");

        for (int i = 0; i < array[1].length; i++) {
            for (int j = 0; j < array.length; j++) {
                System.out.print(array[j][i] + " ");
            }

            System.out.println();
        }
    }

    public static int checkInt(Scanner scan) {
        int width = 0;
        if (scan.hasNextInt()) {
            width = scan.nextInt();
            return width;
        } else {
            scan.next();
            return 0;
        }

    }

}// end of public class Multiply

1 个答案:

答案 0 :(得分:0)

变量的值&#34; j&#34; (第82行)在其最后一次迭代中导致Out of Bounds Exception。 (3x3)(3x2)发生错误,因为在最后的&#34; j&#34;迭代,j等于2,使值大于1(j的位置的最高索引值)。添加一些逻辑以检查何时j>最高指数将阻止这个问题。