Results 1 to 7 of 7
Thread: Comparable checks
- 12-07-2011, 02:21 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 57
- Rep Power
- 0
Comparable checks
hi i am trying to implement a sorted tree structure where each treenode element must be Comparable. The problem i am having is in the constructors.
I first have a constructor such that:
Java Code:public SortedTreeNode(Comparable element, ArrayList<SortedTreeNode> offspring) { this.offspring = offspring; this.element = element; }
Java Code:public SortedTreeNode() { this.offspring = new ArrayList(); this.element = new Comparable(); }
Thanks.
- 12-07-2011, 02:33 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
Re: Comparable checks
Comparable is an interface, rather than a class. So you have to specify the behaviour of a Comparable thing if you create one. (Likewise if a class implements Comparable you have to give the specific Comparable behaviour of that class.) In the jargon Comparable is said to be "abstract": meaning that the compiler will know that element has a compareTo() method, but are supposed to actually define it - or use a class that already implements Comparable. The error you get is the compiler saying "A comparable what?!".
What did you mean the no argument constructor to do? I can see that it creates a node with no offspring. But what did you expect the payload (element) to be in that case?
- 12-07-2011, 02:34 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
Re: Comparable checks
How can i make the check so that when a tree node is constructed it must be of type Comparable?
- 12-07-2011, 02:41 AM #4
Member
- Join Date
- Jan 2011
- Posts
- 57
- Rep Power
- 0
Re: Comparable checks
the default is supposed to just create a node with empty offspring and an empty element of type comparable.
- 12-07-2011, 02:41 AM #5
Member
- Join Date
- Jan 2011
- Posts
- 57
- Rep Power
- 0
Re: Comparable checks
So element could be anything comparable eg. Integer, String ...
- 12-07-2011, 03:14 AM #6
Re: Comparable checks
What good is an empty Comparable object? What would it be used for?
Can you set the variable to null?
- 12-07-2011, 05:37 AM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
Re: Comparable checks
The concept of an "empty" object doesn't really make sense. The nearest, I guess, is a variable whose value is null. I agree with Norm's question: what will the element be used for?
(Comparable)null will keep the compiler happy. Although element will have the value null anyway. (If you do that you have to be careful thereafter not to call element.compareTo() since it'll give a null pointer exception.)
Similar Threads
-
Problem With A Program That Checks For User's Name
By Interista in forum New To JavaReplies: 3Last Post: 11-17-2011, 05:01 PM -
Comparable and Comparator
By jeanjiang in forum New To JavaReplies: 7Last Post: 04-23-2011, 08:59 AM -
A String method that checks to see the data is numeric?
By eLancaster in forum New To JavaReplies: 7Last Post: 02-09-2011, 07:32 PM -
Recursive method that checks if the first n elements of an array is sorted?
By matrixcool in forum New To JavaReplies: 8Last Post: 01-15-2011, 10:39 PM -
Java program that checks in which quadrant a given point belongs
By ningning860310 in forum New To JavaReplies: 5Last Post: 09-09-2008, 05:37 PM
Bookmarks