gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 06:34:51 28 4
gpt4 key购买 nike

我正在从我的教科书中复制一个示例,但它拒绝编译。我是不是在某个地方写错了?由于某种原因,在客户端代码上,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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com