为什么我无法对此ArrayList进行排序?

时间:2014-06-23 05:47:45

标签: java sorting arraylist interface comparable

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

该程序应该创建一个CalendarDate对象以放入ArrayList。通过实现CalendarDate的Comparable接口,我可以使用Collections.sort按顺序在该arraylist中对生日进行排序。但是,这不起作用b / c Collections.sort(日期)将无法运行。

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

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;
    }
    }

Comparable界面:

 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)

2 个答案:

答案 0 :(得分:6)

从您的问题来看,这些是主要内容:

  

然而,这不起作用b / c Collections.sort(日期)将无法运行。

Collections.sort(dates); // WHY WON'T THIS WORK?

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.
}

Collections#sort适用于List<T>,其中T实施java.lang.Comparable。看起来您正在创建自己的Comparable界面,但这不起作用。删除界面Comparable的定义,然后重试。注意:您不需要导入java.lang.Comparable接口,因为它属于java.lang包,并且此包中的类由Java编译器自动添加。

答案 1 :(得分:6)

不要定义自己的Comparable界面。您需要implement java.lang.Comparable