Solved

implementing the Comparable interface

Posted on 2004-10-16
3
225 Views
Last Modified: 2010-03-31
Hi,

I'm making a data structure I've called OrderedLinkedList - which as  you might've guessed is an implementation of a sorted doubly-linked list.

The list accepts Objects, and wraps them in a private class called Node, which just keeps track of the object, the one before it in the list and the one after it in the list.

in my insert(Object o) method, I naturally need to compare Node's in a form of

int result = thisObjectsNode.compareTo(NodeAlreadyInTheList);
if (result == 0)
    //insert this object right here
if (result<0)
   //search behind in the list
if (result>0)
   //search farther in the list

Now, the problem is implementing Comparable. The method compareTo in Node simply calls the compareTo method for the Objects the Nodes of the two operands are wrapping. To clarify, the code is:

public int compareTo(Object n) {
     return(key.compareTo(((Node)n).getKey()));
}

where key is the current Node's Object and getKey returns the other Node's Object.

This generates a compile time error that there is no such method compareTo(Object) in the Object class. But, any Object that will be put into my OrderedLinkedList is expected to implement Comparable and be the same as all other Objects in the list. I however cannot address the Objects as their real class name because I want this to be reusable - so ANY type of Object that implements comparable can use the list. I need the compiler to trust me that whatever Object is calling compareTo is in fact a subclass of Object which DOES have a compareTo method.

If I'm not being clear I'll try to explain better - any help is appreciated.


0
Comment
Question by:phavardel
  • 2
3 Comments
 
LVL 1

Author Comment

by:phavardel
ID: 12328019
Okay, I'm starting to think this was a really dumb question, and the answer is to accept & pass Comparables instead of Objects, since I'm requiring they all be Comparable anyways.

Whoops....
0
 
LVL 24

Accepted Solution

by:
sciuriware earned 250 total points
ID: 12328137
No, that was NOT a dumb question.
The class implements comparable in order for a sorter to be able to call  'compareTo()'

So,   "implements Comparable'   forces you to provide the method     'compareTo()'.
And the compiler will be content.
I just wonder what 'key' is (a String?).

You want to allow any kind of Object to be stored? Then they ALL must be comparable,

The best thing you can do is :

public abstract class MyObject implements Comparable
{
    public abstract int compareTo(MyObject m);
}

... and derive all classes involved from this.

;JOOP!
0
 
LVL 1

Author Comment

by:phavardel
ID: 12328222
Well, I'd like this to be useable on anything comparable - including classes that are not my own like Integer and such.

For what I'm using this for today, key is actually gonna be another kind of Node in another kind of data structure, which will implement Comparable as well (when I make it). The key in that will be a char, so basically this is just a very roundabout alphabetizer, today. But, if I do it right it'll be reusable for any comparable, and we all love reusability.

I just changed it so everything that I previously had Object as an argument or return value for is now Comparable, except the Nodes compareTo() method which has to take Object to properly overload Comparable's compareTo(), and then just casts it as a Node.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now