【发布时间】:2016-08-26 18:18:00
【问题描述】:
我希望有人帮助我重写此方法,使其能够在无法找到匹配项时找到匹配项
public boolean equals(Object otherObject) {
if (otherObject ==null)
{
return false;
}
else if (getClass() != otherObject.getClass())
{
return false;
}
else
{
Contact otherContact = (Contact)otherObject;
return (first.equals(otherContact.first) &&
last.equals(otherContact.last)&&
phone.equals(otherContact.phone)&&
email.equals(otherContact.email));
} }
它显示我的输入中有匹配项时没有匹配项
public void displayMatch()
{
System.out.println("Enter keyword: ");
Scanner input = new Scanner(System.in);
String in = input.next();
for (Contact c : contacts)
{
if (in.equals(c)) {
System.out.println(c);
} else {
System.out.println("No matches.");
}
}
}
【问题讨论】:
-
您试图将
String与Contact进行比较,这对我来说没有意义。 -
带关键字的数组列表内容
-
这个“关键字”代表什么?