gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 12:45:03 26 4
gpt4 key购买 nike

这是我到目前为止得到的代码,但是我的程序说我无法在“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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com