[英]Why can't I call Collections.sort() on my ArrayList<T>?
[英]Why Can't I Sort this ArrayList?
我要從我的教科書中復制一個示例,但是它拒絕編譯。 我在某處打錯字了嗎? 由於某些原因,在客戶端代碼上,Collections.sort(words)不允許程序進行編譯。 任何幫助表示贊賞。 代碼是從Stuart Reges和Marty Stepp的“ Building Java Programs”第二版中復制的。 我試圖通過復制來理解它。
該程序應該創建一個CalendarDate對象以放入ArrayList中。 通過為CalendarDate實現Comparable接口,我可以使用Collections.sort在該arraylist中對生日進行排序。 但是,這不起作用b / c 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;
}
}
可比接口:
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)
根據您的問題,這些是主要部分:
但是,這不起作用b / c Collections.sort(dates)將無法運行。
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編譯器會自動添加此包中的類。
不要定義自己的Comparable
接口。 您需要implement
java.lang.Comparable
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.