I’m using Java 6.
I’m having trouble getting my inner class to use the same generic class as its enclosing class. Currently I have
public class TernarySearchTree < T > {
...
protected class TSTNode < T > {
// index values for accessing relatives array
protected static final int PARENT = 0, LOKID = 1, EQKID = 2, HIKID = 3;
protected char splitchar;
protected TSTNode < T > [] relatives;
private T data;
protected TSTNode(char splitchar, TSTNode < T > parent) {
this.splitchar = splitchar;
relatives = new TSTNode[4];
relatives[PARENT] = parent;
}
}
}
Right now I get the warning
The type parameter T is hiding the type T
If I remove the type parameter from the inner class (i.e. remove the <T> from teh protected class TSTNode<T> line), then I get a compile error on the line relatives = new TSTNode[4].
How can I make everything right?
You can either:
remove the
<T>type parameter fromTSTNode(i.e., make it non-generic) – it will still have access to the outer<T>.rename the
<T>type parameter in classTSTNodeto (say)U.[UPDATE]
Below are four different ways to rewrite your code. All of them compile. I think you should consider the use of an
EnumMap(see Version 4, below).Version 1: use a differenly named type parameter in the inner class. you need to use a List instead of an array.
Version 2: inherit T from enclosing class
Version 3: use a Map (instead of a List)
Version 4: define the indices as an enum + use an EnunMap (instead of a hash map)
[Update 2]
One thing to keep in mind: Use EnumMap instead of ordinal indexing