【问题标题】:Do I have to CompareTo implementation?我必须要比较实现吗?
【发布时间】:2014-12-17 19:39:14
【问题描述】:

我正在尝试在列表中搜索,但我排序为数组,以便我将链接列表转换为数组列表,但是当我在下面没有这部分的情况下编译它时。命令提示符给出“Person 不是抽象的,并且不会覆盖 Comparable 中的抽象方法 compareTo(Person)”。

我该如何解决这个问题?

public int compareTo(Person other){
    if (!this.name.equalsIgnoreCase(other.name))
        return this.name.compareTo(other.name);

        return this.name + " "+other.name;
}

搜索列表和排序方法:

public void searchList(String search)
{
    if(phoneList.size() == 0){
        System.out.println("There is no record phone book.");
    }

    Node<Person> tempNode = phoneList.head;
    SLinkedList<Person> tempList = new SLinkedList();
    for(int i=1; i<=phoneList.size; i++) 
    {
        if (tempNode.getElement().getName().contains(search) || tempNode.getElement().getSurname().contains(search) || tempNode.getElement().getAddress().contains(search) || tempNode.getElement().getCell().contains(search) || tempNode.getElement().getHome().contains(search) || tempNode.getElement().getWork().contains(search))
        {
            tempList.addLast(tempNode.getElement());

            personArray = new Person[tempList.size()];
            Iterator<Person> it = tempList.iterator();

            while(it.hasNext()){
            int x = 0;
            personArray[x] = it.next();
            x++;
            }

            bubbleSort(personArray );
            for(int x = 0; x < tempList.size(); x++) 
            System.out.println((x+1) + ""+ personArray[x]);

        }

        tempNode = tempNode.getNext();
    }
}

public <AnyType extends Comparable<? super AnyType>> void bubbleSort(AnyType[] a) {
    int outer, inner;
    for (outer = a.length - 1; outer > 0; outer--) { // counting down
        for (inner = 0; inner < outer; inner++) { // bubbling up
            if (a[inner].compareTo(a[inner + 1]) > 0) { // if out of order...
                //then swap
                swapReferences(a,inner,inner+1);
            }
        }
    }
}

    public <AnyType> void swapReferences( AnyType [ ] a, int index1, int index2 )
{
    AnyType tmp = a[ index1 ];
    a[ index1 ] = a[ index2 ];
    a[ index2 ] = tmp;
}

人员类别:

public class Person implements Comparable<Person>
{
    private String name;
    private String surname;
    public  String address;
    public  String cell;
    public  String home;
    public  String work;


    public Person(String name, String surname, String address, String cell, String home, String work)
    {
        this.name    = name;
        this.surname = surname;
        this.address = address;
        this.cell    = cell;
        this.home    = home;
        this.work    = work;
    }

    // Accessor methods:
    public String getName(){
        return name;
    }
    public String getSurname(){
        return surname;
    }
    public String getAddress(){
        return address;
    }
    public String getCell(){
        return cell;
    }
    public String getHome(){
        return home;
    }
    public String getWork(){
        return work;
    }

    // Modifier methods:
    public  void setName(String name){
        this.name = name;
    }
    public void setSurname(String surname){
        this.surname = surname;
    }
    public void setAddress (String address){
        this.address = address;
    }
    public void setCell (String cell){
        this.cell = cell;
    }
    public void setHome (String home){
        this.home = home;
    }
    public void setWork (String work){
        this.work = work;
    }

    public String toString(){
        return name + " " + surname + " " + address + " " + cell + " " + home + " " + work;
    }

    public int compareTo(Person other){
        if (!this.name.equalsIgnoreCase(other.name))
            return this.name.compareTo(other.name);

        return this.name + " "+other.name;
    }

}

【问题讨论】:

  • compareTo 应该返回 int 而不是字符串。
  • 好的,但是如何覆盖 compareTo 方法,我搜索它但找不到正确的答案。
  • 当你为一个类实现 compareTo 时,你也应该重写 equals

标签: java methods linked-list abstract compareto


【解决方案1】:

您现有的 compareTo 方法有问题,但删除它违反了implements Comparable 合同,因为您必须提供 compareTo 方法。

public int compareTo(Person other) {
    if (!this.name.equalsIgnoreCase(other.name))
        return this.name.compareTo(other.name);

    // next line returns a String, but the method needs to return an int
    return this.name + " " + other.name;
}

您可以更直接地依赖标准 String compareTo:

public int compareTo(Person other) {
    if ( this.name.equalsIgnoreCase( other.name ) ) { return 0 };

    return this.name.compareTo( other.name );
}

如果您没有编写过的 ignore case 约束,那么这只是

public int compareTo(Person other) {
    return this.name.compareTo( other.name );
}

顺便说一句,没有理由将地址、手机、家庭和工作公开——这通常是不好的做法。

【讨论】:

  • @rumay 不客气。也许你可以投票并接受答案。
【解决方案2】:

为了实现一个接口,您需要实现该接口中的所有方法。您可以删除 implements Comparable 部分或将 public int compareTo 方法添加到您的类。

compareTo 方法的规则是:
- 如果此人大于其他人,则返回 1
- 如果此人小于其他人,则返回 -1
- 如果相等,返回 0

【讨论】:

  • public int compareTo(Person other) 在 Person 类中。是返回值不好。
  • 但他说当他从类中删除该方法时出现错误。
  • 我尝试添加 compareTo 方法,我到处搜索,但找不到正确的答案。
  • 是的。那和@Radiodef 对该问题的评论涵盖了问题所在。 With 方法有一个编译错误,因为在声明 int 时返回一个 String,Without 方法你会因为没有实现它而得到错误。
  • 谢谢!但我试了一下,它给出了“错误的操作数类型错误”。
猜你喜欢
  • 2011-12-22
  • 1970-01-01
  • 2011-08-17
  • 2013-10-21
  • 1970-01-01
  • 2015-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多