一些与二维数组相关的java问题的编译过程

问题描述 投票:0回答:1
public static boolean ContainedArr(boolean[][] photo, boolean[][] sub) {    //find if sub is sub array in photo 
    if (sub.length>photo.length ||sub[0].length>photo[0].length) {  //sub bigger than photo ->return false
        return false;
    }
    for (int i=0;i<photo.length-sub.length+1;i++) { //i runs on rows of photo
        for (int j=0;j<photo[0].length-sub[0].length+1;j++) {   //j runs in cols of photo
            if (photo[i][j]==sub[0][0]) {
                boolean flag =true;
                int row=0,col=1;        //of sub
                while (flag) {
                    if (row==sub.length-1&&col==sub[0].length-1)    
                        return true;
                    if (sub[row][col]==photo[i+row][j+col]) {
                        if (col==sub[0].length-1) {         //last col
                            col =0;
                            row++;
                        }
                        else {
                            col++;
                        }
                    }
                    else
                        flag =false;
                }
            }
        }
    }
    return false;
}

我只是不明白这个结构的逻辑,这个函数的迭代过程是从:

if (row==sub.length-1&&col==sub[0].length-1
到最后。

java syntax compilation iteration sub-array
1个回答
0
投票

大致上 while 循环可以写成一个单独的函数:

boolean isInnerPicture(...) {
    for (row=0; row<sub.length; row++) {
        for (col=0; col<sub[0].length; col++) {
            if (sub[row][col]!=photo[i+row][j+col]) {
                return false;
            }
        }
    }
    return true;
}
© www.soinside.com 2019 - 2024. All rights reserved.