【发布时间】:2021-03-05 00:56:20
【问题描述】:
我不断得到所有测试的“错误”输出,但我不确定我的错误在 equals() 方法的哪个位置。
我知道我必须检查每个对象以确保它们的大小相同,然后如果它们是相同的,则必须检查其中的元素以查看它们是否相同。我认为这就是我正在做的事情,因为 size() 方法返回 manyItems 而 manyItems 是元素的数量。除非我应该在 for 循环中使用 data.length?
我应该得到假,假,真,但相反,我的输出是假,假,假。
见下面代码(测试用的equals()方法和main方法在代码末尾):
//implementation of IntArrayBag copied with equals method added to it
public class intArrayBag implements Cloneable {
private int[] data;
private int manyItems;
//initialize an empty bag with an initial capacity of 10
//postcondition: this bag is empty and has an initial capacity of 10
//throws: OutOfMemoryError - indicates insufficient memory for new int[10]
public intArrayBag() {
final int INITIAL_CAPACITY = 10;
manyItems = 0;
data = new int[INITIAL_CAPACITY];
}
//add new elements to this bag
//parameters: elements - one or more new elements that are being inserted
//postcondition: a new copy of the element has been added to this bag
//throws: OutOfMemoryError - indicates insufficient memory for increasing the bag's capacity
public void addMany(int... elements) {
//if new elements were to take this bag beyond its current capacity, the capacity is increased before adding the new elements
if (manyItems + elements.length > data.length) {
//ensure twice as much space as needed
ensureCapacity((manyItems + elements.length)*2);
}
System.arraycopy(elements, 0, data, manyItems, elements.length);
manyItems += elements.length;
}
//determine the number of elements in this bag
//returns: number of elements in this bag
public int size() {
return manyItems;
}
//equals method to compare bags
//parameters: b - new bag to compare
//returns: if b and the bag that activates this method have exactly the same elements, then return true otherwise the method returns false
public boolean equals(intArrayBag b) {
boolean isEqual = false;
intArrayBag testBag = new intArrayBag();
if (b.size() == testBag.size()) {
for (int i = 0; i < b.size(); i++) {
if (b.data[i] == testBag.data[i]) {
isEqual = true;
} else {
isEqual = false;
break;
}
}
} else {
isEqual = false;
return isEqual;
}
return isEqual;
}
//main method, used for testing equals method
public static void main(String[] args) {
intArrayBag bag1 = new intArrayBag();
bag1.addMany(8, 9, 5, 2, 7, 3, 0, 1, 6);
intArrayBag bag2 = new intArrayBag();
bag2.addMany(5, 7, 6, 1, 0, 6, 7, 3, 9);
//comparison test should return false
System.out.println("comparison test1: " + bag1.equals(bag2));
intArrayBag bag3 = new intArrayBag();
bag3.addMany(2);
intArrayBag bag4 = new intArrayBag();
bag4.addMany(2, 5, 9);
//comparison test should return false
System.out.println("comparison test2: " + bag3.equals(bag4));
intArrayBag bag5 = new intArrayBag();
bag5.addMany(1, 2, 3);
intArrayBag bag6 = new intArrayBag();
bag6.addMany(1, 2, 3);
//comparison test should return true
System.out.println("comparison test3: " + bag5.equals(bag6));
}
}
【问题讨论】:
-
包含很多的代码。那是实际有问题的代码和重点问题吗?这应该可以重现为一个相对较小的 sn-p,包括 实际状态。
-
@user2864740 我说这是代码末尾的 equals() 和 main 方法,问题在于 equals() 方法中的循环中的内容。我不确定它是否设置正确,因为它只返回 false。
-
该方法实例化一个新的空
IntArrayBag然后与它进行比较是完全没有意义的。 -
此外:在编写比较逻辑时,不要乱用局部变量:直奔主题。 “如果大小不等,
return false。”第一次发现不相等的值时,return false。只有在没有崩溃的情况下到达行尾,你才会return true。 -
请浏览此链接,因为您是论坛的新贡献者,因此请确保您提出的问题包含所有良好的描述和错误详细信息,stackoverflow.com/help/how-to-ask