java 错误信息:Collections 类型中的 sort(List<T>) 方法不适用于参数 (ArrayList<Date>)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35688550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 00:27:31  来源:igfitidea点击:

Error Message: The method sort(List<T>) in the type Collections is not applicable for the arguments (ArrayList<Date>)

javacollectionsinterface

提问by JKS7

Keep getting error message, but don't know why. Can't get code to sort the list using Collections.sort()

不断收到错误消息,但不知道为什么。无法获取使用 Collections.sort() 对列表进行排序的代码

Here is what I have. 3 java files.

这是我所拥有的。3个java文件。

The Interface file.

接口文件。

public interface Comparable<T> {
   public int compareTo(T other);
}

The Class file.

类文件。

public class Date implements Comparable<Date>{

private int year;
private int month;
private int day;

public Date(int month, int day, int year){   
   this.month = month;
   this.day = day;
   this.year = year;
}

public int getYear(){
   return this.year;
}

public int getMonth(){
   return this.month;
}

public int getDay(){
   return this.day;
}
public String toString(){
   return month + "/" + day + "/" + year;
}


public int compareTo(Date other){
    if (this.year!=other.year){
        return this.year-other.year;
    } else if (this.month != other.month){
        return this.month-other.month;
    } else {
        return this.day-other.day;
    }
}

}

}

The Client class

客户类

import java.util.*;

public class DateTest{
   public static void main(String[] args){

  ArrayList<Date> dates = new ArrayList<Date>();
  dates.add(new Date(4, 13, 1743)); //Jefferson
  dates.add(new Date(2, 22, 1732)); //Washington
  dates.add(new Date(3, 16, 1751)); //Madison
  dates.add(new Date(10, 30, 1735)); //Adams
  dates.add(new Date(4, 28, 1758)); //Monroe     


  System.out.println(dates);
  Collections.sort(dates);
  System.out.println("birthdays = "+dates);

}




}

The error message I get is "The method sort(List) in the type Collections is not applicable for the arguments (ArrayList)"

我得到的错误消息是“类型集合中的方法 sort(List) 不适用于参数 (ArrayList)”

采纳答案by Sleiman Jneidi

Because Collections.sortexpects java.lang.Comparableand not your Comparable interface, change your Dateclass to implement the java.lang.Comparable.

因为Collections.sortexpectsjava.lang.Comparable而不是您的 Comparable 接口,请更改您的Date类以实现java.lang.Comparable.

public class Date implements java.lang.Comparable<Date>{
 ..
}

If you still want to define your own Comparable for some reasons and you still want to use Collections.sort then the your Comparablehas to be java.util.Comparable

如果由于某些原因你仍然想定义你自己的 Comparable 并且你仍然想使用 Collections.sort 那么你Comparable必须是java.util.Comparable

interface Comparable<T> extends java.lang.Comparable<T> {

}

回答by Lajos Arpad

Your problem is that with the line of

你的问题是

import java.util.*;

you import java.util.Dateas well. Dateis therefore ambivalent :)

你也导入java.util.DateDate因此是矛盾的:)