【发布时间】:2015-05-23 20:29:00
【问题描述】:
我只是想在客户列表中查找对象的位置,尽管当我使用 equals() 方法时它不断返回 false。
我现在知道您必须创建自己的 equals 方法来覆盖自动方法,但是当我在 Customer 数组中比较 Customer 时,我不明白如何创建一个。
这是我的实例变量:
private Customer[] data;
private int size;
private final static int INITIAL_SIZE = 20;
这是我找到对象位置的方法:
public int findCustomerLocation(String name)
{
int spot = -1;
Customer cus = new Customer(name);
for(int i = 0; i < size ; i++)
{
if((data[i].equals(cus)))
{
spot = i;
System.out.println("spot is:" + spot);
}
else
{
System.out.println("spot not found");
}
}
return spot;
}//findCustomerLocation
(未找到返回点)
我正在尝试重写 equals 方法,但我有点卡住了,如果它是另一个实例,我正在尝试使用,但它仍然返回 false
【问题讨论】:
-
显示您的
Customer课程。 -
@ElliottFrisch 不,他正在尝试为数组实现
contains()。问题是他的equals()方法坏了。