Solved

implementing the Comparable interface

Posted on 2004-10-16
3
235 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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…
Suggested Courses

622 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