Java 在二维数组中查找重复数

Java 在二维数组中查找重复数,java,arrays,algorithm,data-structures,Java,Arrays,Algorithm,Data Structures,我有一个二维数组,我需要以水平方式打印,也就是说,从上到下呈对角线 public class ArrayExample { public static int[] array = new int[]{{2,1,0,3,1,6,1}, {2,1,0,3,1,6,1},{2,1,0,3,1,6,1},{2,1,0,3,1,6,1}}; public static void main(String[] args) { printArray(4,4); }

我有一个二维数组,我需要以水平方式打印,也就是说,从上到下呈对角线

public class ArrayExample {

    public static int[] array = new int[]{{2,1,0,3,1,6,1}, {2,1,0,3,1,6,1},{2,1,0,3,1,6,1},{2,1,0,3,1,6,1}};
    public static void main(String[] args) {
        printArray(4,4);
    }

    private static printArray(int row, column){
      for (int i=0; i < row; i++){
        for (int j=0; i<column;j++){
          System.out.print(array[i][j]);
        }
        System.out.println();
      }
    }
}
公共类数组示例{
公共静态int[]数组=新int[]{2,1,0,3,1,6,1},{2,1,0,3,1,6,1},{2,1,0,3,1,6,1},{2,1,0,3,1,1},{2,1,0,3,1,1};
公共静态void main(字符串[]args){
打印阵列(4,4);
}
专用静态printArray(int行、列){
对于(int i=0;i//对于左右对角线。
公共静态布尔值IsConsecutiveFor(顺序、数组){
int值=数组[0][order-1];
int j=阶数-1;
int标志=0;
for(int i=0;i=0;i++){
if(数组[i][j]==值){
System.out.println(“匹配:“+i+”、“+j”处的“+array[i][j]”);
值=数组[i][j];
flag=1;
}
j--;
如果(标志==0){
返回false;
}
}

}

要比较两个对角线,您可以简化逻辑,如下所示:

//This loop is to check the constructiveness for left-right diagonal.
//Because all the diagonal element will have same indexes, so (i,i) can be used.
int temp = matrix[0][0];
int counter = 0;
for (int i=0; i<n; i++) {
        if(matrix[i][i] == temp) {
          counter++;
        }
        else {
         temp = matrix[i][i];
       }
       if(counter == consecutiveTimes) {
          break;
       }
    }

//This loop is to check the constructiveness for right-left diagonal.
//Here sum of all the row index and column index will be n-1. n is the size of your square matrix.
int temp = matrix[0][n-1];
int counter = 0;
for(int i=0; i<n; i++) {
        if(matrix[i][n-1-i] == temp) {
          counter++;
        }
        else {
         temp = matrix[i][n-1-i];
       }
       if(counter == consecutiveTimes) {
          break;
       }
    }
//此循环用于检查左右对角线的构造性。
//因为所有对角线元素都有相同的索引,所以可以使用(i,i)。
int temp=矩阵[0][0];
int计数器=0;

对于(int i=0;i您期望得到什么?您得到了什么?程序在水平和垂直方向上运行良好,但在对角线情况下,要比较的元素没有为对角线元素设置。我添加了System.out语句来跟踪程序流。从左上到右下检查主对角线需要单个循环(不是两个嵌套循环),因为要检查的每个元素的行和列都相同。也就是说,要检查的元素是
[0][0]
[1][1]
,等等。您的算法完全错误。要水平查找连续数字,您需要水平扫描数字,这是正确的。要垂直查找连续数字,您需要垂直扫描。但在对角查找连续数字的算法中,您需要水平扫描数字,这不起作用。我建议y你想一想,如果你不得不在没有电脑的情况下做这件事,你会怎么做,然后用你的母语写出你要用的方法。然后把它翻译成Java。@user3386109不,它不是那么简单。你只检查一条对角线,我想问题需要检查所有对角线。你为什么需要这样做。我不确定,是否成功h在stackoverflow上的情况被认为是好的。如果对答案有任何问题,请评论。上述程序不适用于数组新int[]{2,1,0,3,1,6,1},{0,9,6,8,6,0,1},{5,6,1,1,8,2,9},{6,5,6,1,9,1},{1,6,1,1},{1,6,1,1},{1,7},{0,9,6,6,3,5,3,3,3},{3,5},{2,4},4},其中我们有[4]个对角重复[4,7]次的元素。
//This loop is to check the constructiveness for left-right diagonal.
//Because all the diagonal element will have same indexes, so (i,i) can be used.
int temp = matrix[0][0];
int counter = 0;
for (int i=0; i<n; i++) {
        if(matrix[i][i] == temp) {
          counter++;
        }
        else {
         temp = matrix[i][i];
       }
       if(counter == consecutiveTimes) {
          break;
       }
    }

//This loop is to check the constructiveness for right-left diagonal.
//Here sum of all the row index and column index will be n-1. n is the size of your square matrix.
int temp = matrix[0][n-1];
int counter = 0;
for(int i=0; i<n; i++) {
        if(matrix[i][n-1-i] == temp) {
          counter++;
        }
        else {
         temp = matrix[i][n-1-i];
       }
       if(counter == consecutiveTimes) {
          break;
       }
    }