Solved

implementing the Comparable interface

Posted on 2004-10-16
3
230 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Weekend adv creator 3 64
Java basic valueOf question 1 32
Eclipse Java import and method not resolved 4 73
Setup GlassFish 4 34
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

740 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