How come Java doesn't accept my LinkedList in a Generic, but accepts its own?
Posted
by master chief
on Stack Overflow
See other posts from Stack Overflow
or by master chief
Published on 2010-05-06T21:59:12Z
Indexed on
2010/05/06
22:48 UTC
Read the original article
Hit count: 212
For a class assignment, we can't use any of the languages bultin types, so I'm stuck with my own list. Anyway, here's the situation:
public class CrazyStructure <T extends Comparable<? super T>> {
MyLinkedList<MyTree<T>> trees; //error: type parameter MyTree is not within its bound
}
However:
public class CrazyStructure <T extends Comparable<? super T>> {
LinkedList<MyTree<T>> trees;
}
Works. MyTree impleements the Comparable interface, but MyLinkedList doesn't. However, Java's LinkedList doesn't implement it either, according to this. So what's the problem and how do I fix it?
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);
}
© Stack Overflow or respective owner