英文:
Comparison between two int arrays results unexpected
问题
int[][] storeCurrentBoard;
for (int k = 0; k < QList.size(); k++) {
storeCurrentBoard = QList.get(k);
for (int i = 0; i < playboard.length; i++) {
for (int j = 0; j < playboard[i].length; j++) {
if (playboard[i][j] != storeCurrentBoard[i][j]) {
isPresentinList = false;
break;
}
}
}
}
我正尝试比较两个国际象棋棋盘(用 int[][]
表示)。我也在 C++ 中编写了相同的算法,并且在比较棋盘是否相等时没有任何问题。
我正在尝试在此处发布最相关的代码。在比较“二维”数组之间是否有任何遗漏的 Java 基础知识吗?或者是否可能与内存/存储有关,因为它没有解决解决方案的重复项?我尝试为列表中的棋盘分配新内存,但这会导致无休止的循环。
我还尝试了多种比较方式,甚至使用 .equals
、.deepEquals
、每次迭代和集合。
这是用于检查相等性的比较函数的代码片段,每次镜像和旋转时都会被 TrulyUnique 函数调用,我在此比较了名为 storeCurrentBoard
和 playboard
的两个 int[][]
类型的变量。
<details>
<summary>英文:</summary>
I'm trying to compare two chessboards (represented by `int[][]`). I've written the same algorithm in C++ too and had no issues comparing the chessboards for equality.
I'm trying to post the most relevant code here. Are there any Java fundamentals I have missed regarding comparison between 'two dimensional' Arrays? Or could it be memory/storage related because it didn't sort out the duplicates of the solutions? I tried to allocate new memory for the chessboards in the list but this ends in a endless loop.
I also used multiple ways of comparing the boards even with .equals, .deepEquals, for each iteration and sets.
This is the code snippet from the comparison function that checks for equality and is called by the TrulyUnique Function each time I mirror and rotate. I'm comparing 2 variables of type `int[][]` called `storeCurrentBoard` and `playboard`.
int [][] storeCurrentBoard;
for(int k = 0; k < QList.size(); k++)
{
//isPresentinList = true;
storeCurrentBoard = QList.get(k);
for(int i = 0; i < playboard.length; i++)
{
for(int j = 0; j < playboard[i].length; j++)
{
if(playboard[i][j] != storeCurrentBoard[i][j])
{
isPresentinList = false;
break;
}
}
}
}
</details>
# 答案1
**得分**: 0
```java
// You have `isPresentInList = true` commented out. Uncomment it.
// The way you have it right now, if `isPresentInList` ever gets set to `false`,
// it will always remain false, making it so that all your boards afterwards will always not be equal.
boolean isPresentInList;
int[][] storeCurrentBoard;
for (int k = 0; k < QList.size(); k++) {
isPresentInList = true; // This is necessary for everything to work
storeCurrentBoard = QList.get(k);
outer: for (int i = 0; i < playboard.length; i++) {
for (int j = 0; j < playboard[i].length; j++) {
if (playboard[i][j] != storeCurrentBoard[i][j]) {
isPresentInList = false;
break outer;
}
}
}
}
英文:
You have isPresentInList = true
commented out. Uncomment it. The way you have it right now, if isPresentInList
ever gets set to false
, it will always remain false, making it so that all your boards afterwards will always not be equal.
boolean isPresentInList;
int [][] storeCurrentBoard;
for(int k = 0; k < QList.size(); k++)
{
isPresentinList = true; //This is necessary for everything to work
storeCurrentBoard = QList.get(k);
outer: for(int i = 0; i < playboard.length; i++)
{
for(int j = 0; j < playboard[i].length; j++)
{
if(playboard[i][j] != storeCurrentBoard[i][j])
{
isPresentinList = false;
break outer;
}
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论