不确定如何修复我的矩阵代码的错误,静态数组的超出界限错误

时间:2017-01-17 08:25:29

标签: arrays matrix indexoutofboundsexception

我正在尝试创建一个代码,根据用户输入的代码填充两个矩阵,然后使用这些矩阵添加它们减去它们转置第一个矩阵并将它们相乘;但是,我继续在第18行获得一个超出范围的例外matrix1[m][n] = matrix[m][n];。另外,我不明白如何使用用户输入的变量创建公共数组的边界。任何帮助都会很精彩 PS:我真的很抱歉,如果这件事真的很容易,我很傻,而且我也尽最大努力格式化这个,如果我搞砸了什么就不要生气。

import java.util.Scanner;

public class Matrix {
private int m,n,p,q,c,d,e,f,sum = 0,a,b;
int matrix1[][] = new int[m][n];
int matrix2[][]= new int [m][n];
public Matrix(){

}
//this portion is meant to fill the arrays matrix1 and matrix2 to be used later
public void fillarray(){
    Scanner in = new Scanner(System.in);
      System.out.println("Enter the number of rows and columns of matrix A");
      m = in.nextInt();
      n = in.nextInt();
      //out of bounds error here when attempting to declare length of rows and columns
      int matrix [][]= new int [m][n];
      matrix1[m][n] = matrix[m][n];

      System.out.println("Enter the elements of matrix A");

      for ( c = 0 ; c < m ; c++ ){
         for ( d = 0 ; d < n ; d++ ){
           matrix1[c][d] = in.nextInt();
         }
      }
      System.out.println("Enter the number of rows and columns of matrix A");
      p = in.nextInt();
      q = in.nextInt();
      //same out of bounds error here
      matrix2[p][q] = matrix2[p][q];

      System.out.println("Enter the elements of matrix B");

      for ( e = 0 ; e < p ; c++ ){
         for ( f = 0 ; f < q ; d++ ){
           matrix2[e][f] = in.nextInt();
         }
      }
}
//meant to transpose the array
    public void getTranspose(){
        int transpose[][] = new int[n][m];

          for ( c = 0 ; c < m ; c++ )
          {
             for ( d = 0 ; d < n ; d++ )               
                transpose[d][c] = matrix1[c][d];
          }

          System.out.println("Transpose of entered matrix: ");

          for ( c = 0 ; c < n ; c++ )
          {
             for ( d = 0 ; d < m ; d++ )
                   System.out.print(transpose[c][d]+"\t");

             System.out.print("\n");
          }
       }
    //meant to add the two matrices and find the sum
    public void addMatrix(){
          int sum[][] = new int[m][n];

          for ( c = 0 ; c < m ; c++ ){
              for ( d = 0 ; d < n ; d++ ){
                  sum[c][d] = matrix1[c][d] + matrix2[c][d];
              }
          }
          System.out.println("Sum of entered matrices: ");
          for ( c = 0 ; c < m ; c++)
          {
             for ( d = 0 ; d < n ; d++ ){
                System.out.print(sum[c][d]+"\t");
             }
             System.out.println();
          }
    }
    //meant to subtract the two matrices and find the difference
    public void subtractMatrix(){
        int sum[][] = new int[m][n];
        for ( c = 0 ; c < m ; c++ ){
              for ( d = 0 ; d < n ; d++ ){
                  sum[c][d] = matrix1[c][d] - matrix2[c][d];
              }
          }
        System.out.println("Differences of entered matrices: ");
          for ( c = 0 ; c < m ; c++ ){
             for ( d = 0 ; d < n ; d++ ){
                System.out.print(sum[c][d]+"\t");
             }
             System.out.println();
          }

    }
    //meant to multiply the two matrices and if the sizes do not match then explain why
    public void multiplyMatrix(){
        int multiply[][] = new int[m][q];
        if ( n != p ){
             System.out.println("Matrices with entered orders can't be multiplied with each other.");
             for ( c = 0 ; c < m ; c++ ){
                for ( d = 0 ; d < q ; d++ ){   
                   for ( int k = 0 ; k < p ; k++ ){
                      sum = sum + matrix1[c][k]*matrix2[k][d];
                   }

                   multiply[c][d] = sum;
                   sum = 0;
                }
             }

             System.out.println("Product of entered matrices:-");

             for ( c = 0 ; c < m ; c++ ) {
                for ( d = 0 ; d < q ; d++ ){
                   System.out.print(multiply[c][d]+"\t");
                }
                System.out.print("\n");
             }

        }
    }

}

这是我的测试程序:

public class MatrixTest { 
     public static void main (String[] args){
         Matrix a = new Matrix(); 
         a.fillarray(); 
         a.getTranspose(); 
         a.addMatrix();
         a.subtractMatrix();
         a.multiplyMatrix(); 
     } 
}   

0 个答案:

没有答案