New to java, I did search for examples, but I’m having a really hard time.
I have a node class that uses generics:
public class Node<T> {
private int key;
private T data;
private Node<T> nextNode;
}
The node is Okay.
public class GenericDictionary implements GenericDictionary_interface {
private int capacity;
private Node<T> [] slots;
public void add (Node<T> newNode) {
}
}
This is how I would write it, I think. I want the GenericDictionary to work with node objects.
I get an error: “T cannot be resolved as a Type”.
Thing is I don’t know how it should look like.
Your
GenericDictionaryclass would need to be generic as well:At that point, it’ll be fine to use
Node<T>within the class.If the interface contains the
addmethod, the interface should probably be generic too:I’d advise against an interface called
GenericDictionary_interface, by the way… if there’s only ever going to be one implementation, you could call the interfaceGenericDictionaryand the implementationGenericDictionaryImpl– somewhat horrible, but reasonably common. If there are multiple implementations, name the implementation by some differentiating aspect.