两个整数数组之间的比较结果出现了意外情况。

huangapple go评论100阅读模式
英文:

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 函数调用,我在此比较了名为 storeCurrentBoardplayboard 的两个 int[][] 类型的变量。


<details>
<summary>英文:</summary>

I&#39;m trying to compare two chessboards (represented by `int[][]`). I&#39;ve written the same algorithm in C++ too and had no issues comparing the chessboards for equality. 
 
I&#39;m trying to post the most relevant code here. Are there any Java fundamentals I have missed regarding comparison between &#39;two dimensional&#39; Arrays? Or could it be memory/storage related because it didn&#39;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&#39;m comparing 2 variables of type `int[][]` called `storeCurrentBoard` and `playboard`.

            int [][] storeCurrentBoard;
			for(int k = 0; k &lt; QList.size(); k++)
			{
				//isPresentinList = true;
				storeCurrentBoard = QList.get(k);

				for(int i = 0; i &lt; playboard.length; i++) 
				{
					for(int j = 0; j &lt; 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 &lt; QList.size(); k++)
    {
        isPresentinList = true; //This is necessary for everything to work
        storeCurrentBoard = QList.get(k);

        outer: for(int i = 0; i &lt; playboard.length; i++) 
        {
            for(int j = 0; j &lt; playboard[i].length; j++)
            {
                if(playboard[i][j] != storeCurrentBoard[i][j])
                {
                    isPresentinList = false;
                    break outer;
                }
            }
        }
    }

huangapple
  • 本文由 发表于 2020年5月5日 02:41:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/61599345.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定