What signature should I have on my insert
-method? I'm struggling with the generics. In a way, I want both Comparable<T>
and T
and I have tried with <Comparable<T> extends T>
.
public class Node<T> {
private Comparable<T> value;
public Node(Comparable<T> val) {
this.value = val;
}
// WRONG signature - compareTo need an argument of type T
public void insert(Comparable<T> val) {
if(value.compareTo(val) > 0) {
new Node<T>(val);
}
}
public static void main(String[] args) {
Integer i4 = new Integer(4);
Integer i7 = new Integer(7);
Node<Integer> n4 = new Node<>(i4);
n4.insert(i7);
}
}