I'm attempting to write code for a BinarySearchTree in Java. I keep getting a runtime error when I try to test it by instantiating an Integer BST. Here's the relevant code:
public class BinarySearchTree<E extends Comparable<E>> {
private E[] nodes;
@SuppressWarnings("unchecked")
public BinarySearchTree() {
nodes = (E[])new Object[10];
}
}
Now, I have this line in main:
BinarySearchTree<Integer> test = new BinarySearchTree<Integer>();
When I run the code I get this error linked to the first line of the constructor:
[Ljava.lang.Object; cannot be cast to [Ljava.lang.Comparable;
I'm not very familiar with generics, but I'm guessing this is because Object doesn't have a defined compareTo method? If this line won't work, what other options do I have so that I can have an array that can somehow hold the generic Comparable types?