Java - 二维数组检查对角线数字板

Fli*_*nze 5 java arrays 2d multidimensional-array

目前我正在开发一个在 8x8 2D 阵列板中生成随机 0 和 1 的程序。我要做的是检查对角线上的所有数字是否相同(从角开始,而不仅仅是任何对角线)。

例子:

int[][] array = {
    {0, 0, 0, 0, 0, 0, 0, 1},
    {0, 0, 1, 0, 1, 0, 1, 0},
    {0, 0, 0, 0, 1, 1, 1, 0},
    {0, 0, 0, 0, 1, 1, 1, 0},
    {0, 0, 1, 1, 0, 1, 1, 0},
    {0, 0, 1, 0, 0, 0, 1, 0},
    {0, 1, 0, 0, 0, 0, 0, 0},
    {1, 0, 0, 1, 1, 1, 1, 0}
};
Run Code Online (Sandbox Code Playgroud)

因此,如果碰巧从左上角 (0,0),(1,1)...(7,7) 开始的所有数字都是 0 或 1,那么我必须输出到扫描仪,指示“有是 0" 的主对角线(来自上面的示例)。

同样从这个例子中,我们可以看到,从右上角开始,数字“1”向左下角对角重复,那么我还必须显示“有一个小对角线为1”。

到目前为止,我已经弄清楚如何生成数字并将其输入数组,但我不知道如何检查。这是我到目前为止所拥有的:

public class JavaTest{
// Main method
public static void main(String[] args) {

    int[][] array = {
        {0, 0, 0, 0, 0, 0, 0, 1},
        {0, 0, 1, 0, 1, 0, 1, 0},
        {0, 0, 0, 0, 1, 1, 1, 0},
        {0, 0, 0, 0, 1, 1, 1, 0},
        {0, 0, 1, 1, 0, 1, 1, 0},
        {0, 0, 1, 0, 0, 0, 1, 0},
        {0, 1, 0, 0, 0, 0, 0, 0},
        {1, 0, 0, 1, 1, 1, 1, 0}
    };

    // Print array numbers
    for (int i = 0; i < array.length; i++) {
        for (int j = 0; j < array[i].length; j++)
            System.out.print(array[i][j] + " ");
        System.out.println();
    }
    // Print checkers

    checkMajorDiagonal(array);
}
// Check major diagonal 
public static void checkMajorDiagonal(int array[][]) {
    int majDiag;
    boolean isMatching = true;
    int row = 0;
    for(row = 0; row < array.length; row++){
        majDiag = row;
        if(array[row][row] != array[row+1][row+1]){
            isMatching = false;
            break;
        }
    }
    //If all elements matched print output
    if(isMatching)  
        System.out.println("Major diagonal is all " + array[row][row]);
    }
}
Run Code Online (Sandbox Code Playgroud)

虽然到目前为止我所做的并没有像我想要的那样工作,因为存在错误,而且我仍然必须做小对角线。提前致谢。

rga*_*ber 4

已经有一堆答案了。这是另一种方法。您走在正确的轨道上,但没有必要通过检查对角元素与下一个元素等来使事情复杂化。只需检查每个对角线元素与第一个对角线元素。一旦发现差异,就停止检查!

 public static void checkDiagonal(int[][] array){

     // Start with the assumption that both diagonals are consistent.
     boolean majorConsistent = true; 
     boolean minorConsistent = true;

     int length = array.length;

     int tempMajor = array[0][0];        // all elements in the Major must be equal to this
     int tempMinor = array[0][length-1]; // all elements in the Minor must be equal to this

     // Check major diagonal, and update the boolean if our assumption is wrong.
     for(int i=0; i<length; i++){ 
         if (array[i][i] != tempMajor) { //(0,0);(1,1);(3,3);...
             majorConsistent = false;
             break;
         }
     }

     // Check minor diagonal, and update the boolean if our assumption is wrong.
     for(int i=0,j=length-1; i<length; i++,j--){
         if (array[i][j] != tempMinor) { //(0,7);(1,6);(2,5);...
             minorConsistent = false;
             break;
         }
     }

     System.out.println("Major elements all same = "+majorConsistent);
     System.out.println("Minor elements all same = "+minorConsistent);

 }
Run Code Online (Sandbox Code Playgroud)

这样你仍然可以在O(n)中进行检查,并且不需要嵌套 for 循环!请注意,您可以优化此代码以删除冗余,即有一个 for 循环等。