首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在大型7*7矩阵中遍历3* 3子矩阵时绑定异常错误的数组索引

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

Stack Overflow用户
提问于 2019-08-31 14:34:35
回答 1查看 35关注 0票数 1

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

代码语言:javascript
复制
                [(0,1),(0,2),(0,3)]
                [(1,1),(1,2),(1,3)]
                [(2,1),(2,2),(2,3)]

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

代码语言:javascript
复制
                 [(0,2),(0,3),(0,4)]
                 [(1,2),(1,3),(1,4)]
                 [(2,2),(2,3),(2,4)]

我的代码

代码语言:javascript
复制
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);
     }
  }
}
}

运行此代码时出现错误,如下所示

代码语言:javascript
复制
ArrayIndexOutOfBoundsException: -1 

请帮帮忙

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-08-31 14:58:39

代码中的IndexOutOfBoundsException来自您调用p = m + (p - 1)。您不需要在循环的每次迭代中更改pq变量。

此外,您有几个不必要的变量,并且其中一些是静态的,当您只在这样的循环中使用它们时,应该避免使用这些变量。在清理了代码的格式并删除了所有不必要的变量之后,我相信代码可以像您希望的那样工作。

代码忽略随机矩阵的第一行和第一列。这是我们想要的行为吗?

代码语言:javascript
复制
import java.util.Random;

public class MatrixTest {

    public static void subMatrix(int startRow, int startCol, int[][] mat) {

        System.out.print("Submatrix for : ");
        System.out.println(startRow + ", " + startCol);
        shiftMatrix(startRow, startCol, mat);
    }

    public static void shiftMatrix(int startRow, int startCol, int[][] mat) {
        int[][] d = new int[3][3];
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                //to properly move within the 3x3 you only need to add a 
                //constant buffer to the indices of mat[][]
                d[i][j] = mat[i + startRow][j + startCol]; 
            }
        }
        System.out.println("Your 3*3 SubMatrix is : ");
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                System.out.print(d[i][j] + "\t");
            }
            System.out.println();
        }
    }
    public static void main(String[] args) {

        int[][] mat = new int[7][7];
        for (int i = 0; i < 7; i++) {
            for (int j = 0; j < 7; j++){
                Random rand = new Random();
                mat[i][j] = rand.nextInt(10);
            }
        }

        //copying large matrix to another for passing by argument 

        System.out.println("Copied matrix is : ");
        for (int i = 0; i < 7; i++){
            for (int j = 0; j < 7; j++) {
                System.out.print(mat[i][j] + "\t");
            }
            System.out.println();
        }
        //Here is the 3*3 submatrix traversing starts...

        for (int i = 1; i < 5; i++) { //changed from i < 6 to i < 5 to stay inside 7x7
            for (int j = 1; j < 5; j++) {
                subMatrix(i, j, mat);
            }
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57735759

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档