java - 为什么我无法对这个 ArrayList 进行排序?

标签 java sorting arraylist interface comparable

我正在从我的教科书中复制一个示例,但它拒绝编译。我是不是在某个地方写错了?由于某种原因,在客户端代码上,Collections.sort(words) 不允许程序编译。任何帮助表示赞赏。代码复制自 Stuart Reges 和 Marty Stepp 的《Building Java Programs》第二版。我试图通过复制它来理解它。

该程序应该创建一个 CalendarDate 对象并将其放入 ArrayList 中。通过实现 CalendarDate 的 Comparable 接口(interface),我可以使用 Collections.sort 在该数组列表中按顺序对生日进行排序。但是,这不起作用,因为 Collections.sort(dates) 将不会运行。

客户端代码(包含问题):

import java.util.*;

// Short program that creates a list of birthdays of the
// first 5 U.S. Presidents and that puts them into sorted order.
// We can now use Collections.sort for ArrayList<CalendarDate> b/c CalendarDate implements the Comparable interface. 

public class CalendarDateTest {
    public static void main(String[] args) {
        ArrayList<CalendarDate> dates = new ArrayList<CalendarDate>(); // Creates a new ArrayList of 'CalendarDate' object type.

        // adds a new CalendarDate object with month = 2 and day = 22 into an element of ArrayList dates, and etc.
        dates.add(new CalendarDate(2, 22)); // Washington
        dates.add(new CalendarDate(10, 30)); //Adams
        dates.add(new CalendarDate(4, 13)); // Jefferson
        dates.add(new CalendarDate(3, 16)); // Madison
        dates.add(new CalendarDate(4, 28)); // Monroe

        System.out.println("birthdays = " + dates); // Before sorting
        Collections.sort(dates); // WHY WON'T THIS WORK?
        System.out.println("birthdays = " + dates); // After Sorting
    }

}

CalendarDate 对象类:

 public class CalendarDate implements Comparable<CalendarDate> { 
    private int month;
    private int day;

    // Constructor
    public CalendarDate(int month, int day) {
        this.month = month;
        this.day = day;
    }

    // Compares this calendar date to another date
    // Dates are compared by month and then by day
    public int compareTo(CalendarDate other) {
        if (month != other.month) { // If different months
            return month - other.month; //negative, positive, or zero
        } else { // If same months; compare days instead
            return day - other.day; // negative, positive, or zero
        }
    }

    // Accessor for month (b/c month is private)
    public int getMonth() {
        return this.month;
    }

    // Accessor for day (b/c day is private)
    public int getDay() {
        return this.day;
    }

    // A toString method
    public String toString() {
        return month + "/" + day;
    }
    }

可比较接口(interface):

 public interface Comparable<T> { // T is the generic type (a placeholder for when other classes implement this)
        public int compareTo(T other); // placeholder to be implemented; need more specific version into class implementing this.
    }

编译器错误:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    Bound mismatch: The generic method sort(List<T>) of type Collections is not applicable for the arguments (ArrayList<CalendarDate>). The inferred type CalendarDate is not a valid substitute for the bounded parameter <T extends Comparable<? super T>>

    at CalendarDateExample.CalendarDateTest.main(CalendarDateTest.java:21)

最佳答案

不要定义自己的Comparable接口(interface)。您需要实现 java.lang.Comparable .

关于java - 为什么我无法对这个 ArrayList 进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24359130/

相关文章:

java - 方法参数数组列表变量的大小在clear()后重置为零并分配新的列表值

java - 如何停止打印所有迭代并仅显示真实迭代

java - isReachable 方法

java - while 循环之后的代码永远不会执行

python - 关于 Python 内置的 sort() 方法

javascript - 使用indexOf时数组排序不正确/不可预测?

java - netty中如何动态管理CPU负载?

java - 如何阻止 tomcat 尝试连接到本地 memcached 服务器失败?

xslt - 保留 xsl :sort'd nodes 之间的空格

java - Arraylist 将值相乘在一起未按预期执行