java:sortedList类比较错误

java:sortedList类比较错误,java,Java,我有这两个类,其中一个是sortedLinkedList类,它具有一个泛型类型的compariable。当我主要实施它们时: SortedListInterface<Apartment> aptList=new SortedLinkedList<Apartment>(); SortedLinkedListclass: /**A class that holds Apartment informations*/ public class Apartment{ pr

我有这两个类,其中一个是
sortedLinkedList
类,它具有一个泛型类型的
compariable
。当我主要实施它们时:

SortedListInterface<Apartment> aptList=new SortedLinkedList<Apartment>();
SortedLinkedList
class:

/**A class that holds Apartment informations*/
public class Apartment{

   private String id;
   private int yearsLeft;

//class methods

}//end Apartment class
/**
 * A class that implements the ADT sorted list by using a chain of nodes.
 * Duplicate entries are allowed.
 *
 * @author Frank M. Carrano
 * @version 2.0
 */
public class SortedLinkedList<T extends Comparable<? super T>>
             implements SortedListInterface<T>
{
    //class methods
} // end SortedLinkedList
/**
*通过使用节点链实现ADT排序列表的类。
*允许重复条目。
*
*@作者Frank M.Carrano
*@version 2.0
*/

公共类SortedLinkedList您将
SortedLinkedList
定义为
SortedLinkedList您将
SortedLinkedList
定义为
SortedLinkedList,以便能够使用
公寓
SortedLinkedList
, 需要实现
compatible
接口,例如:

  class Apartment implements Comparable<Apartment> {
    @Override
    public int compareTo(Apartment o) {
      return 0;
    }

    // ... the rest of your code
  }
要按
yearsLeft
的相反顺序订购,您可以这样写:

@Override
public int compareTo(Apartment o) {
  return Integer.compare(yearsLeft, o.yearsLeft);
}
@Override
public int compareTo(Apartment o) {
  return -Integer.compare(yearsLeft, o.yearsLeft);
}

您可以在.

中阅读更多有关
可比
界面的信息,以便能够将
单元
分类链接列表
一起使用, 它需要实现
可比
接口,例如:

  class Apartment implements Comparable<Apartment> {
    @Override
    public int compareTo(Apartment o) {
      return 0;
    }

    // ... the rest of your code
  }
要按
yearsLeft
的相反顺序订购,您可以这样写:

@Override
public int compareTo(Apartment o) {
  return Integer.compare(yearsLeft, o.yearsLeft);
}
@Override
public int compareTo(Apartment o) {
  return -Integer.compare(yearsLeft, o.yearsLeft);
}

您可以在中阅读有关
Comparable
接口的更多信息。

只需将类
SortedLinkedList
定义为具有条件类型参数的泛型类
T扩展Comparable只需将类
SortedLinkedList
定义为具有条件类型参数
T的泛型类即可延伸可比