这是我的学校作业。我不知道如何解决它,因为该方法仅允许传递一个参数。请给我一些提示。
这是一个问题:
添加方法public boolean等于(对象其他),该方法在2个Alist对象的内容相同时返回true。请注意,如果有2个对象具有相同数量的项目,并且一个对象中的每个项目都等于其在另一个对象中的相应位置(15分)。
上面的方法必须在llist2.java
中添加alist类
public class AList<T> implements ListInterface<T> {
private T[] list; // array of list entries
private int numberOfEntries;
private static final int DEFAULT_INITIAL_CAPACITY = 25;
public AList() {
this(DEFAULT_INITIAL_CAPACITY); // call next constructor
} // end default constructor
public AList(int initialCapacity) {
numberOfEntries = 0;
// the cast is safe because the new array contains null entries
@SuppressWarnings("unchecked")
T[] tempList = (T[]) new Object[initialCapacity];
list = tempList;
} ......
llist2.java
public class LList2<T> implements ListInterface<T> {
private Node firstNode; // head reference to first node
private Node lastNode; // tail reference to last node
private int numberOfEntries;
public LList2() {
clear();
} // end default constructor
public final void clear() // NOTICE clear is not final in interface and that is OK
{
firstNode = null;
lastNode = null;
numberOfEntries = 0;
} ......
这是一个实例方法 - 它将传递的对象与被调用的对象进行比较(又称this
)。请注意,您不需要明确调用this
,但我将其留在那里以使代码更清晰:
@Override
public boolean equals(Object other) {
// Check that other is even an AList
if (!(other instanceof AList)) {
return false;
}
// If it is, cast it and compare the contents:
AList otherAList = (AList) other;
// Compare the lenghts of the arrays
if (this.numberOfEntires != otherAList.numberOfEntries) {
return false;
}
// Compare the contents of the arrays:
for (int i = 0; i < this.numberOfEntries; ++i) {
if (!this.list[i].equals(otherAList.list[i])) {
return false;
}
}
// Didn't find a reason why the two aren't equal, so they must be:
return true;
}