java - 如何使用 Arrays.equals 确定 2 个 2d 数组是否相同(意味着它们具有相同的数字但顺序不同)?在JAVA中

标签 java arrays

这是我到目前为止得到的代码,但是我的程序说我无法在“for(int i:m1)”行将 int[] 转换为 int

public static boolean equals(int [][] m1, int [][] m2)
       {
          boolean val = false;
          int ct = 0;
          for(int i:m1)
          {
             for(int row = 0; row < m1.length; row++)
             {
             for(int column = 0; column < m1[row].length; column++)
             {
                val = false;
                for(int row2 = 0; row2 < m2.length; row2++)
                {
                   for(int column2 = ct; column2 < m2[row2].length; column2++)
                   {
                      if(m2[column2] == row2)
                      {
                         val = true;
                         ct++;
                         break;
                      }
                   }
                }
             }
           }
          }
          return val;      

Im using 2 classes for this program. IdenticalArrays is the other class, and equals is the method within that class that determines if the arrays are equal to each other. Right now my compiler is saying that it cant find the symbol and hsa an arrow point to the . in "IdenticalArrays.equals". Why would it not see understand what that is?

if(IdenticalArrays.equals == true)
      {
         System.out.print("The two arrays are identical.");
      }
      else if(IdenticalArrays.equals == false)
      {
         System.out.print("The two arrays are not identaical.");
      }

最佳答案

这应该完全满足您的需要。希望对您有帮助!

public class Program {
static int[][] arr1 = { { 6, 7, 8, 9, 10 }, { 11, 12, 13, 14, 15 } };
static int[][] arr2 = { { 7, 8, 9, 10, 6 }, { 15, 14, 13, 12, 11 } };

public static void main(String[] args) {
    int lowest;
    int switcher;
    int posX = -1;
    int posY = -1;

    for (int i = 0; i < arr2.length; i++) {
        for (int z = 0; z < arr2[i].length; z++) {
            lowest = arr2[i][z];

            for (int x = i; x < arr2.length; x++) {
                if (x == i)
                    for (int y = z; y < arr2[x].length; y++) {
                        if (arr2[x][y] <= lowest) {
                            lowest = arr2[x][y];
                            posX = x;
                            posY = y;
                        }
                    }
                else
                    for (int y = 0; y < arr2[x].length; y++) {
                        if (arr2[x][y] <= lowest) {
                            lowest = arr2[x][y];
                            posX = x;
                            posY = y;
                        }
                    }
                ;
            }
            switcher = arr2[i][z];
            arr2[i][z] = arr2[posX][posY];
            arr2[posX][posY] = switcher;


        }
    }

    System.out.println(equals(arr1, arr2)); 

}

public static boolean equals(int[][] m1, int[][] m2) {
    for (int x = 0; x < m1.length; x++) {
        for (int y = 0; y < m1[x].length; y++) {
            if (m1[x][y] != m2[x][y]) {
                return false;
            }
        }
    }

    return true;
 }
}

关于java - 如何使用 Arrays.equals 确定 2 个 2d 数组是否相同(意味着它们具有相同的数字但顺序不同)?在JAVA中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36416127/

相关文章:

java - 如何强制子类作为参数(或不允许父类作为参数)

java - 防止 Spring 预填充字段

javascript - 在将输出返回给用户 JS 之前过滤掉已删除的结果

java - 使用 RestTemplate 和 RestTemplateCustomizer 与多个服务的正确架构是什么?

java.lang.IllegalArgumentException : Result Maps collection already contains value for 异常

java - 如何用更具体的类型替换参数化类型

javascript - JS-JSON : adding a keypair in each object by calculating the difference in time now and a timestamp in the JSON object

javascript - 来自关联数组的树

arrays - 从 NSUserDefaults 检索数组后出现错误

c - 缓冲文件中的一组行并将其存储在 C 中的数组中