如何在二维数组检查,如果行和列的所有元素都是一样的吗? [英] How to check in 2d array if all elements in row and column are the same?

查看:130
本文介绍了如何在二维数组检查,如果行和列的所有元素都是一样的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用Java编写一个code如果在一个行和列的所有元素都是相同的,将外观和打印出它的索引。

I am trying to write a code in java that would look if all elements in a row and column are the same and print out the indexes of it.

因此​​,举例来说,如果我有二维数组

So for example if I had two dimensional array of

{ 1, 0, 0, 0, 0}
{ 1, 0, 0, 0, 0}
{ 1, 1, 1, 1, 1}

欲打印出2,0,这意味着行2中的所有元素是相同的,在第0列中的所有元素都相同的太并且它们彼此相等。

I want to print out 2,0 which means that in the row 2 all the elements are the same and in column 0 all the elements are the same too and they are equal to each other.

我试图与语句来做到这一点。

I was trying to do it with for statements

int[][] array_1 = { {1}, 
                    {1} };
for(int row=0; row<array.length-1; row++){
        for (int col=0; col<array[0].length-1; col++){
              if(array[row][col]==array[row+1][col]&&
                 array[row][col]==array[row][col+1]&&
                 array[row][col]==array_1[0][0]) {
                  System.out.print(row);
                  System.out.println(col);      
                 }
        }
}

但它不工作,因为它不检查所有的行和列,某处停在中间,我不知道那是什么发生。

but it does not work because it does not check all the rows and columns, it stops somewhere in the middle and I have no idea what that is happening.

你有什么建议吗?

推荐答案

您可以通过分离方法做到这一点。结果
首先,你可以写一个TRANSPOSE(移调)方法

You can do this by separating the methods.
First you can write a tranpose method

public static int[][] transpose(int[][] A) {
        int m = A.length;
        int n = A[0].length;
        int[][] C = new int[n][m];
        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++)
                C[j][i] = A[i][j];
        return C;
    }

然后,你可以写此方法来检查行和columns.If行的元素或列的元素不同,此方法返回false.Else返回true。

Then,you can write this method to check the rows and the columns.If row's elements or column's elements are different,this method return false.Else return true.

public static boolean rowColEquals(int[] array) {
        for (int i = 1; i < array.length; i++) {
            int x = array[0];
            if (x != array[i])
                return false;
        }
        return true;
    }

和最后,你可以写这个方法来找到答案。

And Finally, you can write this method to find answer.

public static void findEquals(int[][] array) {
        for (int i = 0; i < array.length; i++) {
            if (rowColEquals(array[i])) {
                System.out.println("All " + array[i][0] + " is equal on row "
                        + i);
            }
        }
        array = transpose(array);
        for (int i = 0; i < array.length; i++) {
            if (rowColEquals(array[i])) {
                System.out.println("All " + array[i][0] + " is equal on colomn "
                        + i);
            }
        }
    }

主要方法如下

public static void main(String[] args) {
int[][] y = { { 1, 0, 0, 0, 0 }, { 1, 0, 0, 0, 0 }, { 1, 1, 1, 1, 1 } };
        findEquals(y);
    }

和输出将被以下。

All 1 is equal on row 2
All 1 is equal on colomn 0

这篇关于如何在二维数组检查,如果行和列的所有元素都是一样的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆