Solved

implementing the Comparable interface

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

Technology Partners: 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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

751 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