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的泛型类即可延伸可比