【问题标题】:Method that checks whether two arrays have the same elements in the same order检查两个数组是否具有相同顺序的相同元素的方法
【发布时间】:2014-02-20 03:32:51
【问题描述】:

我首先要说我已经在这个问题上工作了好几个小时,但没有成功。我搜索了互联网,但没有找到任何帮助。

问题很简单: 写一个方法

 public static boolean equals(int[] a, int[] b)

检查两个数组是否具有相同顺序的相同元素。

到目前为止,我对数组没有任何问题,我开始怀疑整个班级是否有问题。

这是我当前的代码,是我整个晚上都尝试过的许多变体之一,它们都给出了相同的结果。我意识到这可能不是最好的语法,但这是我上次 google 搜索的结果。

int[] a = {1, 4, 9, 16, 9, 7, 4, 9, 11};
int[] b = {1, 4, 9, 16, 7, 7, 4, 9, 11};

public static boolean equals(int[] a, int[] b) { 
 if (a.length != b.length)
       return false;
     for (int i = 0; i < a.length; i++)
       if (a[i] != b[i])
         return false;
     return true;
   }

如您所见,数组仅相差一个元素。该方法每次都会返回 false,无论它们是否相等。我用过

Arrays.equals(a,b);

Arrays.equals(a[i], b[i]);

每次都返回 false。我正在使用 Dr. Java,交互面板允许您输入几乎任何表达式。多次我在交互面板中声明了两个数组,然后使用上面的第一种方法比较它们,它返回 true。

在这一点上,我完全不知所措。感谢您提供任何帮助,如果之前有人问过这个问题,我深表歉意,但我觉得我的情况是独一无二的。

编辑:

System.out.println( "equals(): " + equals(a, b)  );

是对equals方法的调用。

编辑#2:

这是课程的完整代码,这是一个预先制作的作业模板,这就是我避免发布整个内容的原因。

有问题的方法是 P6.9

导入 java.util.Arrays;

class Homework5 {

public static void main(String[] args) {

 int[] a = {1, 4, 9, 16, 9, 7, 4, 9, 11};
 int[] b = {1, 4, 9, 16, 7, 7, 4, 9, 11};

 //P6.6
 System.out.println( "P66(): " + P66(a) ); // should print: P68(): -2 

 //P6.7
 System.out.println( "P67(): " + prettyPrint( P67(a) ) );   //should print: P67(): [11, 9, 4, 7,      9, 16, 9, 4, 1] 

 //P6.9
 System.out.println( "equals(): " + equals(a, b)  ); // should print: P69(): false 

 //P6.10
 System.out.println( "sameSet(): " +  sameSet(a, b)  ); // should print:  P610(): true 

}

//this method can be used to see a nice visual representation of an array of ints
public static String prettyPrint(int[] array) {
 if (array == null) {
 return "null"; 
 }
 String answer = "[";
 for (int i=0; i< array.length; i++) {
 answer += array[i] + ", ";
 }
 answer = answer.substring(0, answer.length() -2);
 answer += "]";
 return answer; 
 }


//***************************
//*****  Problem P6.6  ******
//***************************

//Problem P6.6
public static int P66(int[] array) {
  int sum = array[0];
  for (int i = 0; i < (array.length - 1); i++) {
    if ((i % 2) == 0) {
      sum -= array[i + 1];
    }
    else {
      sum += array[i + 1];
    }
  }
 return sum;
 }

//***************************
//*****  Problem P6.7  ******
//***************************

//Problem P6.7
public static int[] P67(int[] array) {
  if ((array.length % 2) == 0) {
   for (int i = 0; i <= ((array.length / 2 ) - 1); i++) {
   int a = array[i];
   int b = array[array.length - (i + 1)];
   array[i] = b;
   array[array.length - (i + 1)] = a;
  }
  }
  else {
   for (int i = 0; i <= (array.length / 2 ); i++) {
   int a = array[i];
   int b = array[array.length - (i + 1)];
   array[i] = b;
   array[array.length - (i + 1)] = a;
  }
  }
 return array;
 }

//***************************
//*****  Problem P6.9  ******
//***************************

//Problem P6.9
public static boolean equals(int[] a, int[] b) { 
  if (a.length != b.length)
       return false;
     for (int i = 0; i < a.length; i++)
       if (a[i] != b[i])
         return false;
     return true;
   }

//***************************
//*****  Problem P6.10  *****
//***************************

//Problem P6.10
public static boolean sameSet(int[] a, int[] b) {
return false;
}

}

【问题讨论】:

  • Multiple times I have declared both arrays in the interactions panel, and then compared them using the first method above, and it returns true 展开
  • 在调试器中运行程序并手动单步执行循环。找出if 语句在何时评估为true 并返回false。此外,如果您在交互面板中声明数组,然后重新运行代码,代码仍然使用声明中的值,而不是您在 IDE 界面中键入的任何值。
  • 我从类中删除了该方法,并将其放在自己的类中。我将数组和代码复制到新类中。将数组设置为相等,运行代码,结果为真。问题必须出在类本身,然后正确吗?
  • 你如何调用你的 equals 方法?也发布该代码。
  • “当类本身运行时”是什么意思?对于失败的情况,您可以发布整个课程吗?因为,正如您已经说过的,该方法本身可以正常工作。

标签: java arrays boolean


【解决方案1】:

您对P67 的调用正在修改您传递给它的数组。因此,当您调用equals 时,数组a 中没有原始值。删除上面写着的那一行

System.out.println( "P67(): " + prettyPrint( P67(a) ) );

【讨论】:

  • 太棒了,注释掉该行解决了这个问题。我现在唯一的问题是如何让它与包含的行一起使用?班级必须打印所有四个答案才能获得学分。
  • 你必须按正确的顺序调用这四个方法吗?也许你可以先做equals,然后再做P67?或者,您可以只复制数组a
  • 我宁愿不切换顺序,因为我不相信它是由人类评分的。据我所知,每种方法都通过几个测试用例来检查正确性。复制是指在 P6.7 中复制“a”并使用它吗?
  • 是的,您可以在 P67 中制作副本,这样它就不会修改其输入 - 我不确定这是否符合 P67 应该做的规范。或者,您可以在main 内制作副本,然后将一个副本传递给P67,将另一个副本传递给equals
  • 我将不得不与一位教授核对一下,看看首选什么。谢谢您的帮助!我很高兴知道问题不在于我的方法。
【解决方案2】:

奇怪,因为当我在 Intelli IDEA 中运行代码时 - 它可以工作。基本情况为假。当我将 7 更改为 9 以获得匹配时,我得到了真实的结果。如果我删除一个元素 - 我会得到错误的回报。

我同意可以改进格式,但对我来说,代码有效,除非我在问题中遗漏了什么。

这是我的 IDE 中的代码,有几个小改动:

package util;

/**
 * Created by mike on 2/19/14.
 */
public class SOArrays {

public static int[] a = {1, 4, 9, 16, 9, 7, 4, 9, 11};
public static int[] b = {1, 4, 9, 16, 7, 4, 9, 11};
//public static int[] b = {1, 4, 9, 16, 9, 7, 4, 9, 11};
//public static int[] b = {1, 4, 9, 16, 7, 7, 4, 9, 11};

public static boolean equals(int[] a, int[] b) {
    if (a.length != b.length)
        return false;
    for (int i = 0; i < a.length; i++)
        if (a[i] != b[i])
            return false;
    return true;
}

public static void main(String[] args) {
    System.out.println("equals: "+SOArrays.equals(a, b));
}
}

这奏效了。

【讨论】:

  • 不是一个真正的答案。 OP 想知道为什么他/她的代码不起作用。这只是说“我不知道”。
  • 当我从类中删除该方法并单独运行它时,我也会得到它以产生正确的答案。这让我相信该方法正在调用不正确的数组。唯一的问题是它们是类中唯一的数组。
  • @DavidWallace 我说的是 - 代码有效 - 至少对我来说有效。
  • 换句话说,你不知道为什么当 OP 运行它时它不起作用。我也不知道为什么当 OP 运行它时它不起作用,这就是为什么我还没有发布答案。
  • 好的 - 我明白你的意思。我会将我的“版本”放在回复中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-27
  • 2014-07-29
  • 2012-06-10
  • 2014-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多