【发布时间】: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 方法?也发布该代码。
-
“当类本身运行时”是什么意思?对于失败的情况,您可以发布整个课程吗?因为,正如您已经说过的,该方法本身可以正常工作。