[英]Implementing Comparable with a generic doubly linked list
I'm writing a doubly linked list that as objects extended from my class "Furniture" are added, sorts them using a compareTo method. 我正在编写一个双向链接列表,其中添加了从“家具”类扩展的对象,并使用compareTo方法对它们进行排序。
My list and node codes: 我的列表和节点代码:
class Node<E extends Comparable<E>> { ....... }
class DList<E extends Comparable<E>>
{
[..]
public void add(E c)
{
Node<E> finger = head;
while (finger != null && ((finger.value().compareTo(c)) > 0))
finger = finger.Next();
}
}
My compareTo method(s) are located in subclasses of my Furniture class, for example: 我的compareTo方法位于我的Furniture类的子类中,例如:
class Chair extends Furniture
{
[.....]
public int compareTo(Chair c)
{
if(this.getStyle().equals(c.getStyle()))
return this.getColor().compareToIgnoreCase(c.getColor());
return this.getStyle().compareToIgnoreCase(c.getStyle());
}
}
I then try to make my list by 然后,我尝试通过
DList<Furniture> DD = new DList<Furniture>();
and I receive the error: Type argument furniture is not within type variable E 我收到错误:类型参数家具不在类型变量E中
确保Furniture
实施Comparable<Furniture>
也许您的Forniture
类未实现Comparable<Forniture>
。
Your Furniture class should be abstract and should extend Comparable, your Chair class should implement your Furniture class. 您的Furniture类应该是抽象的,并且应该扩展Comparable,而Chair类应该实现Furniture类。
An alternative is to make your Furniture class implement Comparable. 一种替代方法是使您的Furniture类实现可比较。
<E extends Comparable<E>>
表示E
必须扩展Comparable<E>
,因此请确保Furniture扩展Comparable。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.