简体   繁体   English

联系人不能转换为java.lang.Comparable

[英]Contact cannot be cast to java.lang.Comparable

i have been trying hard to spot my error but to no avail. 我一直在努力发现我的错误,但无济于事。 can someone help me? 有人能帮我吗? I am sure that I am comparing the correct objects 我确定我正在比较正确的对象

    Contact[] sortedList = myContactList.sortContactsByName();
    for (Contact c : sortedList) {
        // TODO using this line means you need to implement what in Contact class?
        // Hint: You need to override a method inherited from Object.

        System.out.println(c);
    }
}



public Contact[] sortContactsByName() {
    Contact[] contactArray = contacts.toArray(new Contact[contacts.size()]);
    // TODO Contact class has to implement Comparable
    // interface for this to work!

    Arrays.sort(contactArray);
    // Don't bother how it works now, sorting in Java
    // will be covered later on.
    return contactArray;
}

} }

public int compareTo(Contact o1){

    return this.name.compareTo( o1.name);

You have to declare the class Contact to implement Comaprable . 您必须声明Contact类以实现Comaprable

class Contact implements Comparable<Contact> {

Just implementing compareTo is not enough without this declaration. 没有此声明,仅实现compareTo是不够的。 Also it is good practice to put an @Override annotation above compareTo . @Override注释放在compareTo上方也是一个好习惯。

The class you want to use the interface Comparable with must implement it. 您要使用与Comparable接口一起使用的类必须实现它。

class Contact implements Comparable

You then implement the method below in the Contact class. 然后,在Contact类中实现下面的方法。 You have to remember that this is the first object calling compareTo on the other object of the same type. 您必须记住,这是第一个对相同类型的其他对象调用compareTo的对象。 So you can't access the private variable of name directly in the parameter, use a getter. 因此,您无法直接在参数中访问name的私有变量,请使用getter。

public int compareTo(Contact o1){ 
    return this.name.compareTo(o1.getName());
}

@user3327196: You will have to implement Comparable interface to your Contact class. @ user3327196:您将必须对您的Contact类实现Comparable接口。 Code is attached below: 代码附在下面:

public class Contact implements Comparable<Contact> {

    private String name;

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    @Override
    public int compareTo(Contact contact){
        return this.name.compareTo(contact.name);
    }

}

there are two solutions for your problem, first one is described by most people here, make your contact to implement Comparable interface, this method is good, but it has some limits, ie if you want to have two lists with your contacts, one sorted by firstname second by lastname, you cant do this with this approach 对于您的问题,有两种解决方案,第一种是大多数人在此处描述的方法,使您的联系人实现Comparable接口,这种方法虽然不错,但是有一定的局限性,即如果您希望与联系人一起拥有两个列表,其中一个排序按姓氏,按姓氏,则无法使用此方法

fortunatelly there is method sort which takes custom comparator as parameter, with this solution your class doesnt need to be comparable , all what you need to do is create your custom comparator ie 幸运的是,有一种方法sort将自定义比较器作为参数,使用此解决方案,您的类无需具有comparable ,您所需要做的就是创建自定义比较器,即

public class Contact{
 private String name;

public static final Comparator<Contact> BY_NAME_ASC = new Comparator<Contact>() {

    @Override
    public int compare(Contact o1, Contact o2) {
        return o1.name.compareTo(o2.name);
    }
};

}

now all what you need to do to sort your array is Arrays.sort(contactArray,Contact.BY_NAME_ASC); 现在,对数组进行排序所需要做的就是Arrays.sort(contactArray,Contact.BY_NAME_ASC);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM