Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

implementing the Comparable interface

Posted on 2004-10-16
3
Medium Priority
?
240 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 1000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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 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:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

926 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