我想比较两个数组使用Object重写的方法。我应该把对象参数投到ListInterface,我似乎无法弄清楚发生了什么。任何帮助,将不胜感激。覆盖对象等于方法
public class AList<T extends Comparable> implements ListInterface <T> {
private T[] list;
private int length;
private static final int MAX_SIZE = 50;
public AList()
{
this(MAX_SIZE);
}
public AList(int maxSize)
{
length = 0;
list = (T[]) new AList[maxSize];
}
public boolean equals(Object other)
{
boolean results = true;
if (list.length == ((ListInterface<T>) other).getLength())
{
for(int i = 0; i < list.length; i++)
{
if(list[i].equals((ListInterface<T>)other[i]))
results = true;
}
}
return results;
}
请提供更多关于什么是错的或者你是如何卡住的细节。请注意,我会首先检查null,然后在检查其他内容之前检查引用是否相等。即if(other == null)返回false;'和if(other == this)返回true;'然后我会在* cast之前检查instanceof *。 – 2014-09-02 02:34:19
AList的一个实例应该可能等于实现ListInterface 的不同类的实例吗?这可能会违反.equals(Object o)不变量,如果a.equals(b)为true,则b.equals(a)也应该为true。 –
Mshnik
2014-09-02 02:34:48