Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Collections.sort对特定对象的ArrayList进行排序_Java_Sorting_Collections_Comparable - Fatal编程技术网

Java 使用Collections.sort对特定对象的ArrayList进行排序

Java 使用Collections.sort对特定对象的ArrayList进行排序,java,sorting,collections,comparable,Java,Sorting,Collections,Comparable,因此,我看到了许多与我的问题类似的问题,但我找不到一个与我的问题完全相同的问题 我有一个联系人对象的ArrayList,我想使用Collections对它们进行排序。排序: public void sortContactArrayList(){ ArrayList<Contact> newList = new ArrayList<Contact>(); Collections.sort(newList); } 但是,当我调用Collections.sor

因此,我看到了许多与我的问题类似的问题,但我找不到一个与我的问题完全相同的问题

我有一个联系人对象的ArrayList,我想使用Collections对它们进行排序。排序:

public void sortContactArrayList(){
    ArrayList<Contact> newList = new ArrayList<Contact>();
    Collections.sort(newList);
}
但是,当我调用Collections.sort(newList)时,我收到一个错误

错误是:


“绑定不匹配:类型集合的泛型方法
排序(List
)不适用于参数(
ArrayList
)。推断的类型联系人不是绑定参数
的有效替代者。如果实现了
Comparable
,则应该可以

以下是我的快速测试代码:

Contact.java:

public class Contact implements Comparable<Contact> {

    private String name;

    public Contact(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    @Override
    public int compareTo(Contact otherContact) {
        return this.getName().compareTo(otherContact.getName());
    }
}
公共类联系人实现可比较{
私有字符串名称;
公共联系人(字符串名称){
this.name=名称;
}
公共字符串getName(){
返回名称;
}
@凌驾
公共int比较(联系其他联系人){
返回此.getName().compareTo(otherContact.getName());
}
}
主要方法:

public static void main(String[] args) {

    ArrayList<Contact> newList = new ArrayList<Contact>();
    newList.add(new Contact("Midgar"));
    newList.add(new Contact("David"));
    Collections.sort(newList);
    System.out.println(newList);
}
publicstaticvoidmain(字符串[]args){
ArrayList newList=新的ArrayList();
添加(新联系人(“Midgar”);
新增(新联系人(“大卫”));
Collections.sort(newList);
System.out.println(newList);
}

该消息意味着
联系人
没有实现
可比
。你确定你重新编译了它,并且你的编译器正在查看正确的.class文件吗?对我来说似乎是正确的。你能发布
联系人
的代码吗?非常感谢!它确实有效,我不知道为什么以前没有。出于某种原因,我不需要改变任何东西,我只是有一天没有使用Eclipse,今天就继续使用它,它成功了。在我发布我的问题之前,我几乎确信代码是正确的,所以我需要有人在我开始指责Eclipse出现问题之前验证我是否正确。谢谢!@Midgar77有时你可以尝试在ecli中清理和重建项目pse,或关闭/重新打开eclipse以纠正错误。
public static void main(String[] args) {

    ArrayList<Contact> newList = new ArrayList<Contact>();
    newList.add(new Contact("Midgar"));
    newList.add(new Contact("David"));
    Collections.sort(newList);
    System.out.println(newList);
}