views:

699

answers:

1

In the tree class I'm suppose to compare two node, for you know searching and adding items. I have some issues with how to make it comparable. When one adds data(generic, anything) to the tree one calls the Tree class which then makes a new Node object. How can I declare the variable data/element in the Node class so that it is of type E (anything) and still Comparable? Seriously, I've tried back and forth without concluding with anything.

+3  A: 

Not everything is Comparable. Your requirement is self-contradictory. You can constrain E to be comparable by declaring the generic parameter like:

< E extends Comparable<E> >

This way, the consumer of the class can use all classes that implement Comparable interface with it. You'll be able to access the compareTo method on things typed E.

Mehrdad Afshari
Perfect! Thank you! No, not everything is. But that would be a requirement of the elements used in the tree. My tiny tree implementation now works and makes sense! Thank you.
Algific