[英]implementing an equals() method to compare contents in two objects filled with integers in java
我不斷得到所有測試的“錯誤”輸出,但我不確定我的錯誤在 equals() 方法中的哪個位置。
我知道我必須檢查每個 object 以確保它們的大小相同,然后如果它們是相同的,則必須檢查內部的元素以查看它們是否相同。 我認為這就是我正在做的事情,因為 size() 方法返回 manyItems 而 manyItems 是元素的數量。 除非我應該在 for 循環中使用 data.length ?
我應該得到假,假,真,但相反,我的 output 是假的,假的,假的。
見下面的代碼(用於測試的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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.