[英]Why Can't I Sort this ArrayList?
I am copying off of an example from my textbook and yet it refuses to compile. 我要从我的教科书中复制一个示例,但是它拒绝编译。 Did I make a typo somewhere?
我在某处打错字了吗? For some reason, on the client code, Collections.sort(words) does not allow the program to compile.
由于某些原因,在客户端代码上,Collections.sort(words)不允许程序进行编译。 Any help is appreciated.
任何帮助表示赞赏。 Code is copied from "Building Java Programs" 2nd edition by Stuart Reges and Marty Stepp.
代码是从Stuart Reges和Marty Stepp的“ Building Java Programs”第二版中复制的。 I'm trying to understand it by copying it.
我试图通过复制来理解它。
The program is supposed to crate a CalendarDate object to put into an ArrayList. 该程序应该创建一个CalendarDate对象以放入ArrayList中。 By implementing the Comparable interface for CalendarDate, I can use the Collections.sort to sort the birthdays in order in that arraylist.
通过为CalendarDate实现Comparable接口,我可以使用Collections.sort在该arraylist中对生日进行排序。 However, that's not working b/c Collections.sort(dates) will not run.
但是,这不起作用b / c Collections.sort(dates)将无法运行。
The Client code(Contains the issue): 客户代码(包含问题):
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
}
}
The CalendarDate object class: 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;
}
}
The Comparable 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.
}
Compiler error: 编译器错误:
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)
From your question, these are the main pieces: 根据您的问题,这些是主要部分:
However, that's not working b/c Collections.sort(dates) will not run.
但是,这不起作用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
will work on a List<T>
where T
implements java.lang.Comparable
. Collections#sort
将在List<T>
上工作,其中T
实现java.lang.Comparable
。 Looks like you're creating your own Comparable
interface, and this won't work. 看来您正在创建自己的
Comparable
接口,但这将无法正常工作。 Remove the definition of your interface Comparable
and try again. 删除接口
Comparable
的定义,然后重试。 Note: you don't need to import java.lang.Comparable
interface since it belongs to java.lang
package, and classes in this package are automatically added by Java compiler. 注意:您不需要导入
java.lang.Comparable
接口,因为它属于java.lang
包,并且Java编译器会自动添加此包中的类。
Don't define your own Comparable
interface. 不要定义自己的
Comparable
接口。 You need to implement
java.lang.Comparable
. 您需要
implement
java.lang.Comparable
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.