重写equals()以便与链表一起使用



我有两种方法来查找链表中特定元素的索引。我正在尝试使用contais()indexOf()来查找索引。我不确定如何覆盖equals()以满足我的需要。一种方法是根据姓氏和首字母来查找索引,另一种方法只是根据电话号码。以下是我的方法:

@Override
public int lookupNumber(String surname, String initials) {
    Entry entry1 = new Entry(surname, initials);
    if (listDirectory.contains(entry1)) {
        int index = listDirectory.indexOf(entry1);
        return index;
    }
    else {
        return -1;
    }
}
@Override
public int lookupName(int extension) {
    Entry entry1 = new Entry(Integer.toString(extension));
    if (listDirectory.contains(entry1)) {
        int index = listDirectory.indexOf(entry1);
        return index;
    }
    else {
        return -1;
    }
}

我尝试使用eclipse提供的equals()hashCode()

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Entry other = (Entry) obj;
    if (extension == null) {
        if (other.extension != null)
            return false;
    } else if (!extension.equals(other.extension))
        return false;
    if (initals == null) {
        if (other.initals != null)
            return false;
    } else if (!initals.equals(other.initals))
        return false;
    if (surname == null) {
        if (other.surname != null)
            return false;
    } else if (!surname.equals(other.surname))
        return false;
    return true;
}

但它不适用于我的两个方法,因为它比较对象中的所有变量,而不仅仅是我需要比较的变量。正确的逻辑是什么?

非常感谢您的帮助,谢谢。

更新-这是我的完整入门课程-

public class Entry { 
private String surname;
private String initals;
private String extension;

public Entry(String surname, String initals,String extension) { 
    this.surname = surname;
    this.initals = initals;
    this.extension = extension;
}

  public Entry(String surname,String initals){
    this.surname = surname;
    this.initals = initals;
}

public Entry(String extension) {
    this.extension = extension;
}
public String getInitals(){
    return initals;
}
public String getSurname(){
    return surname;
}
public String getExtension(){
    return extension;
}
public void setExtension(String extension) {
     this.extension = extension;
}
public String toString(){
    return surname  + "t " + initals + "t" + extension;
}
@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result
            + ((extension == null) ? 0 : extension.hashCode());
    result = prime * result + ((initals == null) ? 0 : initals.hashCode());
    result = prime * result + ((surname == null) ? 0 : surname.hashCode());
    return result;
}
@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Entry other = (Entry) obj;
    if (extension == null) {
        if (other.extension != null)
            return false;
    } else if (!extension.equals(other.extension))
        return false;
    if (initals == null) {
        if (other.initals != null)
            return false;
    } else if (!initals.equals(other.initals))
        return false;
    if (surname == null) {
        if (other.surname != null)
            return false;
    } else if (!surname.equals(other.surname))
        return false;
    return true;
}
}

更新-我的问题是在我的lookupNumber()方法中,我想通过(initials,surname)找到元素,而在lookupNumber()中,我希望通过(extension)找到元素。我该怎么做?

Java Object类包含自己的equals()方法,Entry类应该是Entry类型对象的蓝图。Java继承使得所有类都从Object类继承,这对我们很多人来说用处不大
@Override
public boolean equals(Entry otherEntry)
{
    if(!(this.surname.equals(otherEntry.getSurname()))
        return false;
    else if(!(this.initials.equals(otherEntry.getInitials()))
        return false;
    else if(!(this.extension.equals(otherEntry.getExtension()))
        return false;
    else
        return true;
}

上面的代码只是检查实例变量之间的不平等,如果任何一个不平等,则返回false。

相关内容

  • 没有找到相关文章

最新更新