對於一個類的任務,我們不能使用任何語言bultin類型,所以我堅持我自己的列表。總之,這裏的情況:Java如何不接受通用中的LinkedList,但接受它自己的?
public class CrazyStructure <T extends Comparable<? super T>> {
MyLinkedList<MyTree<T>> trees; //error: type parameter MyTree is not within its bound
}
但是:
public class CrazyStructure <T extends Comparable<? super T>> {
LinkedList<MyTree<T>> trees;
}
作品。 MyTree實現了Comparable接口,但MyLinkedList沒有。但是,根據this,Java的LinkedList也沒有實現它。那麼問題是怎麼解決的?
MyLinkedList:
public class MyLinkedList<T extends Comparable<? super T>> {
private class Node<T> {
private Node<T> next;
private T data;
protected Node();
protected Node(final T value);
}
Node<T> firstNode;
public MyLinkedList();
public MyLinkedList(T value);
//calls node1.value.compareTo(node2.value)
private int compareElements(final Node<T> node1, final Node<T> node2);
public void insert(T value);
public void remove(T value);
}
MyTree:
public class LeftistTree<T extends Comparable<? super T>>
implements Comparable {
private class Node<T> {
private Node<T> left, right;
private T data;
private int dist;
protected Node();
protected Node(final T value);
}
private Node<T> root;
public LeftistTree();
public LeftistTree(final T value);
public Node getRoot();
//calls node1.value.compareTo(node2.value)
private int compareElements(final Node node1, final Node node2);
private Node<T> merge(Node node1, Node node2);
public void insert(final T value);
public T extractMin();
public int compareTo(final Object param);
}
我們可以看到你的MyLinkedList定義嗎? – OscarRyz 2010-05-06 22:02:31
更好地向我們展示MyLinkedList和MyTree的聲明。 – bmargulies 2010-05-06 22:03:00
編輯方法簽名,我可以發佈其餘的,如果你想 – 2010-05-06 22:32:02