简体   繁体   English

为什么这不起作用? 比较两个二维数组中的元素

[英]Why isn't this working? Comparing elements in two 2d arrays

For some reason this will always return 0? 由于某种原因,它将始终返回0? Why? 为什么? I compare two 2d arrays of Space objects, which have a specific colour on them. 我比较了空间对象的两个2d数组,它们具有特定的颜色。 I have made sure the two 2d arrays I am comparing are different but the method won't work? 我已经确定要比较的两个2d数组不同,但是该方法不起作用?

public int compareArray(Space[][] test1, Space[][] test2)
{
    for(int row = 0;row< test1.length;row++)
    {
        for(int column = 0; column<test1.length;column++)
        {
            if(!((test1[row][column]).getColour()).equals(test2[row]    [column].getColour()))
            {
                System.out.println("Found a non match");
                return 1;
            }

        }
    }
    return 0;
}

I call this method here, the evenNumber method, changes test, and makes it different to test2, it's for the project I'm doing 我在这里称此方法为evenNumber方法,更改测试,并使它与test2不同,它用于我正在做的项目

public void testMethods()
   {
       Space[][] test = new Space[4][4];
   Space[][] test2= new Space[4][4];

   for(int i = 0; i < test.length; i++){
         for(int j = 0; j < test.length; j++){
              test[i][j] =  new Space(spaceColour.Null); 
              test2[i][j] =  new Space(spaceColour.Null);
         }
    }


   test[0][1].setColour(spaceColour.White); 
   test[1][1].setColour(spaceColour.White);  
   test[2][1].setColour(spaceColour.Black); 
   test[2][2].setColour(spaceColour.Black); 

   test2[0][1].setColour(spaceColour.White); 
   test2[1][1].setColour(spaceColour.White);  
   test2[2][1].setColour(spaceColour.Black); 
   test2[2][2].setColour(spaceColour.Black); 

  System.out.println(compareArray(test, test2));


   evenNumber(test);
   drawBoard(test);
   System.out.println("");
   drawBoard(test2);

 }
test[0][1].setColour(spaceColour.White); 
test[1][1].setColour(spaceColour.White);  
test[2][1].setColour(spaceColour.Black); 
test[2][2].setColour(spaceColour.Black); 

test2[0][1].setColour(spaceColour.White); 
test2[1][1].setColour(spaceColour.White);  
test2[2][1].setColour(spaceColour.Black); 
test2[2][2].setColour(spaceColour.Black); 

The colors of test1 and test2 are the same. test1test2的颜色相同。 So your if statements evaluates to false all the time, as you are checking if they are not the same. 所以,你的if语句的计算结果为false的时候,如果他们是一样的,你正在检查。

Try with these values: 尝试使用以下值:

test[0][1].setColour(spaceColour.White); 
test[1][1].setColour(spaceColour.White);  
test[2][1].setColour(spaceColour.Black); 
test[2][2].setColour(spaceColour.Black); 

test2[0][1].setColour(spaceColour.Black); 
test2[1][1].setColour(spaceColour.Black);  
test2[2][1].setColour(spaceColour.White); 
test2[2][2].setColour(spaceColour.White); 

I think you need to change the second for loop from 我认为您需要将第二个for循环从

for(int column = 0; column<test1.length;column++)

to

for(int column = 0; column<test1[0].length;column++)

You need to do a little more 你需要做更多

if (test1.length != test2.length) {
  return 1;
}
for(int row = 0;row< test1.length;row++)
{
  if (test1[row].length != test2[row].length) {
    return 1;
  }
  // back to your code
  for(int column = 0; column<test1[row].length;column++)
  {
    if(!((test1[row][column]).getColour()).equals(test2[row][column].getColour()))
    {
      System.out.println("Found a non match");
      return 1;
    }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM