矩阵乘法索引超出范围异常

时间:2014-08-24 16:41:39

标签: java

import java.util.Scanner;
class matrixmultiply
{
    static void matrixmultiply(int x[][],int y[][])
    {
        int i=0;
        int matrix3[][]=new int[x.length][y[i].length];
        for(i=0;i<matrix3.length;i++)
        {
            for(int j=0;j<matrix3[i].length;i++)
            {
                matrix3[i][j]=0;
                for(int ip=0;ip<y.length;ip++)
                {
                    matrix3[i][j]+=x[i][ip]*y[ip][j];
                }
            }
        }
        for(i=0;i<matrix3.length;i++)
        {
            for(int j=0;j<matrix3[i].length;i++)
            {
                System.out.println(matrix3[i][j]+" ");
            }
            System.out.println();
        }
    }

    public static void main(String args[])
    {
        Scanner x=new Scanner(System.in);
        int m,n,p,q;
        System.out.println("enter rows and columns of 1st matrix");
        m=x.nextInt();
        n=x.nextInt();
        int matrix1[][]=new int[m][n];
        System.out.println("enter rows and columns of 2nd matrix");
        p=x.nextInt();
        q=x.nextInt();
        int matrix2[][]=new int[p][q];
        if(n==p)
        {
            System.out.println("matrices can be multiplied");
            for(int i=0;i<matrix1.length;i++)
            {
                for(int j=0;j<matrix1[i].length;i++)
                {
                    matrix1[i][j]=x.nextInt();
                }
            }
            for(int i=0;i<matrix2.length;i++)
            {
                for(int j=0;j<matrix2[i].length;i++)
                {
                    matrix2[i][j]=x.nextInt();
                }
            }
            matrixmultiply(matrix1,matrix2);
        }
        else
            System.out.println("matrices can't be multiplied");
    }
}

出现的问题是:

Exception in thread "main" java.lang.Arrayindexoutofboundsexception: 2
                      at matrixmultiply.main<matrixmultiply.java:46>

2 个答案:

答案 0 :(得分:3)

for(int j=0;j<matrix1[i].length;i++)
                                ^^^

您正在递增i而不是j

答案 1 :(得分:1)

以下是我建议的方式:

package matrix;

import java.io.PrintStream;

/**
 * MatrixUtils description here
 * @author Michael
 * @link https://stackoverflow.com/questions/25473938/i-am-making-a-code-in-java-for-the-multiplication-of-two-matrices-can-anyone-p
 * @since 8/24/2014 12:47 PM
 */
public class MatrixUtils {

    public static void main(String[] args) {
        int [][] a = {
                { 1, 2, 3 },
                { 4, 5, 6 },
                { 7, 8, 9 }
        };
        int [][] b = {
                { 2, 0, 0 },
                { 0, 2, 0 },
                { 0, 0, 2 }
        };

        int [][] result = MatrixUtils.mult(a, b);
        MatrixUtils.print(System.out, result);

    }

    public static void print(PrintStream ps, int [][] m) {
        for (int i = 0; i < m.length; ++i) {
            for (int j = 0; j < m[i].length; ++j) {
                ps.print(String.format("%5d ", m[i][j]));
            }
            ps.println();
        }
    }

    public static int [][] mult(int[][] a, int [][] b) {
        if (a == null) throw new IllegalArgumentException("Matrix a cannot be null");
        if (b == null) throw new IllegalArgumentException("Matrix b cannot be null");
        if (a[0].length != b.length) throw new IllegalArgumentException("Size mismatch");

        int numRows = a.length;
        int numCols = b[0].length;
        int[][] result = new int[numRows][numCols];
        for (int i = 0; i < numRows; ++i) {
            for (int j = 0; j < numCols; ++j) {
                int sum = 0;
                for (int k = 0; k < b.length; ++k) {
                    sum += a[i][k]*b[k][j];
                }
                result[i][j] = sum;
            }
        }
        return result;
    }
}