Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么可以';我不能对这个数组列表进行排序吗?_Java_Sorting_Arraylist_Interface_Comparable - Fatal编程技术网

Java 为什么可以';我不能对这个数组列表进行排序吗?

Java 为什么可以';我不能对这个数组列表进行排序吗?,java,sorting,arraylist,interface,comparable,Java,Sorting,Arraylist,Interface,Comparable,我从课本上抄袭了一个例子,但它拒绝编译。我在什么地方打错了吗?出于某种原因,在客户端代码上,Collections.sort(words)不允许程序编译。感谢您的帮助。代码是从Stuart Reges和Marty Stepp的“Building Java Programs”第二版复制而来的。我试图通过复制来理解它 该程序应该装箱一个CalendarDate对象以放入ArrayList。通过实现CalendarDate的可比较接口,我可以使用Collections.sort在该arraylist中

我从课本上抄袭了一个例子,但它拒绝编译。我在什么地方打错了吗?出于某种原因,在客户端代码上,Collections.sort(words)不允许程序编译。感谢您的帮助。代码是从Stuart Reges和Marty Stepp的“Building Java Programs”第二版复制而来的。我试图通过复制来理解它

该程序应该装箱一个CalendarDate对象以放入ArrayList。通过实现CalendarDate的可比较接口,我可以使用Collections.sort在该arraylist中按顺序对生日进行排序。但是,这不适用于b/c集合。排序(日期)将不会运行

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.
}
客户端代码(包含问题):

import java.util.*;
//创建孩子生日列表的简短程序
//前5任美国总统,这使他们排列有序。
//我们现在可以使用Collections.sort for ArrayList b/c CalendarDate实现可比较的接口。
公共类日历日期测试{
公共静态void main(字符串[]args){
ArrayList dates=new ArrayList();//创建“CalendarDate”对象类型的新ArrayList。
//将月=2、日=22的新CalendarDate对象添加到ArrayList日期的元素中,以此类推。
dates.add(新日历日期(2,22));//华盛顿
dates.add(新日历日期(10,30));//Adams
dates.add(新日历日期(4,13));//Jefferson
dates.add(新日历日期(3,16));//麦迪逊
dates.add(新日历日期(4,28));//梦露
System.out.println(“birthdays=“+dates);//排序前
Collections.sort(dates);//为什么不起作用?
System.out.println(“生日=+日期);//排序后
}
}
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;
    }
    }
公共类CalendarDate实现可比较的{
私人整数月;
私人国际日;
//建造师
公共日历日期(整数月,整数日){
本月=月;
this.day=天;
}
//将此日历日期与其他日期进行比较
//日期按月比较,然后按天比较
公共整数比较(日历日期其他){
if(month!=其他.month){//if不同的月份
返回月份-other.month;//负、正或零
}else{//如果是相同的月份,则比较天数
返回日期-other.day;//负、正或零
}
}
//月访问器(b/c月为专用)
公共整数getMonth(){
本月返回;
}
//日访问器(b/c日为私人日)
public int getDay(){
今天返回;
}
//toString方法
公共字符串toString(){
返回月份+“/”+天;
}
}
可比界面:

 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.
    }
public interface Comparable{//T是泛型类型(其他类实现此功能时的占位符)
public int compareTo(T other);//要实现的占位符;需要在类中实现更具体的版本。
}
编译器错误:

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)
线程“main”java.lang中出现异常。错误:未解决的编译问题:
绑定不匹配:类型集合的泛型方法排序(列表)不适用于参数(ArrayList)。推断类型CalendarDate不能有效替代您问题中的有界参数,以下是主要内容:

但是,这不适用于b/c集合。排序(日期)将不会运行

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.
}
集合。排序(日期);//为什么这样不行?
公共接口{//T是泛型类型(其他类实现此功能时的占位符)
public int compareTo(T other);//要实现的占位符;需要在类中实现更具体的版本。
}

将处理
列表
,其中
T
实现。看起来您正在创建自己的
可比较的
界面,但这不起作用。请删除界面的定义,然后重试。注意:您不需要导入
java.lang.Compariable
接口,因为它属于
java.lang
包,并且该包中的类由java编译器自动添加。

不定义您自己的
Compariable
接口。您需要
实现

提供准确的编译器错误将是一个良好的开端。据我所知,在比较中返回任何正整数或负整数都是不对的。。它应该是-1、0或1。@EdwardM.B。这只是符号/结果是否为零。您是否提供了自己的
Compariable
实现?看起来是这样,因为你发布了代码。如果是这样,请删除它并使用java.lang.Compariable接口。方法
Collections.sort
仅适用于此方法。@JigarJoshi看起来像是OP创建了自己的
Comparable
接口,该接口与
java.lang.Comparable
@JigarJoshi:不,他没有,因为私人成员访问是完全合法的。这是
CalendarDate
类中的代码,试图访问
CalendarDate
私人成员。@JigarJoshi为什么?它在同一个班级里。@JigarJoshi别担心,每个人都会遇到这种情况:)