猿问

遍历大型 7*7 矩阵内的 3* 3 子矩阵时绑定异常错误的数组索引

我想从中间元素(第 2 行,第 2 列)的 (1,1) 起始位置遍历一个大 7*7 矩阵内的 3*3 子矩阵。位置 (1,1) 对应的子矩阵将是

                [(0,1),(0,2),(0,3)]
                [(1,1),(1,2),(1,3)]
                [(2,1),(2,2),(2,3)]

像这样遍历将继续..下一个子矩阵起始位置将是 (1,2)

                 [(0,2),(0,3),(0,4)]
                 [(1,2),(1,3),(1,4)]
                 [(2,2),(2,3),(2,4)]

我的代码

static int i;

static int j;

static int g;

static int h;


static void submatrix(int p,int q,int[][] mat) {


System.out.print("Submatrix for : ");

System.out.println(p+","+q);

shiftmatrix(p,q,mat);

}


static void shiftmatrix(int p,int q,int[][] mat) {

 int m,n;

 int[][] d = new int[3][3];

 for( m=0;m<3;m++) {

  for( n=0;n<3;n++) {

   p=m+(p-1);

   q=n+q;

   d[m][n]=mat[p][q];

     }

   }


System.out.println("Your 3*3 SubMatrix is : ");

    for ( m = 0; m < 3; m++){

    for ( n = 0; n < 3; n++){

        System.out.print(d[m][n]+"\t");

        }

         System.out.println();

        }

}

public static void main(String[] args) {


    int[][] a = new int[7][7];

    int[][] mat = new int[7][7];

    for ( i = 0; i < 7; i++)

      {

       for ( j = 0; j < 7; j++){

          Random rand = new Random();

           a[i][j] = rand.nextInt(10);

          }

      }


//copying large matrix to another for passing by argument 


 System.out.println("Copied matrix is : ");

 for (i = 0; i < 7; i++){

     for (j = 0; j < 7; j++){

          mat[g][h]=a[i][j];

            System.out.print(mat[g][h]+"\t");

        }

          System.out.println();

    }


//Here is the 3*3 submatrix traversing starts...


 for (i=1;i<6;i++) {

   for (j=1;j<5;j++) {

    int p=i;

     int q=j;

     submatrix(p,q,mat);

     }

  }

}

}

运行此代码时出现错误


ArrayIndexOutOfBoundsException: -1 

请帮忙


慕码人2483693
浏览 125回答 1
1回答

潇湘沐

您的代码IndexOutOfBoundsException中的 是您调用的p = m + (p - 1)。您不需要在循环的每次迭代中更改p和变量。q此外,您还有几个不必要的变量,其中一些是静态的,当您仅在这样的循环中使用它们时应该避免这种情况。在清理代码的格式并删除所有不必要的变量之后,我相信代码可以按照您的要求运行。该代码忽略随机矩阵的第一行和第一列。这是期望的行为吗?import java.util.Random;public class MatrixTest {&nbsp; &nbsp; public static void subMatrix(int startRow, int startCol, int[][] mat) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.print("Submatrix for : ");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(startRow + ", " + startCol);&nbsp; &nbsp; &nbsp; &nbsp; shiftMatrix(startRow, startCol, mat);&nbsp; &nbsp; }&nbsp; &nbsp; public static void shiftMatrix(int startRow, int startCol, int[][] mat) {&nbsp; &nbsp; &nbsp; &nbsp; int[][] d = new int[3][3];&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < 3; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < 3; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //to properly move within the 3x3 you only need to add a&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //constant buffer to the indices of mat[][]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; d[i][j] = mat[i + startRow][j + startCol];&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Your 3*3 SubMatrix is : ");&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < 3; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < 3; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(d[i][j] + "\t");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; int[][] mat = new int[7][7];&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < 7; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < 7; j++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Random rand = new Random();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mat[i][j] = rand.nextInt(10);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //copying large matrix to another for passing by argument&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Copied matrix is : ");&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < 7; i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < 7; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(mat[i][j] + "\t");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //Here is the 3*3 submatrix traversing starts...&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 1; i < 5; i++) { //changed from i < 6 to i < 5 to stay inside 7x7&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = 1; j < 5; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; subMatrix(i, j, mat);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答