在java中实现了一个equals()方法来比较两个用整数填充的对象中的内容



我一直得到" false ";输出所有测试,但我不确定在equals()方法我的错误是。

我知道我必须检查每个对象以确保它们是相同的大小,然后如果它们是,则必须检查其中的元素以查看它们是否相同。我认为这就是我所做的,因为size()方法返回许多项目和许多项目是元素的数量。除非我应该使用数据。for循环的长度?

我应该得到false, false, true,但是,我的输出是false, false, false。

参见下面的代码(用于测试的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));

}

}

如果你使用关键字this而不是testBag (intArrayBag testBag = new intArrayBag();),你的算法将工作,这里你实际上创建了一个带有0元素的新变量,并将其与参数进行比较。

同样,你也可以用更短的

public boolean equals(intArrayBag b) {
if (b.size() != this.size()) {
return false;
}

for (int i = 0; i < b.size(); i++) {
if (b.data[i] != this.data[i]) {
return false;
}
}

return true;
}

最新更新