File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Object Comparable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Object Comparable" Watch "Object Comparable" New topic
Author

Object Comparable

Timothy Leong
Ranch Hand

Joined: May 25, 2005
Posts: 55
Hi all, I have this linked list class which needs to insert nodes of different objects (eg Person, Company, Invoice) in ascending order.

I get this error:
List is not abstract and does not override abstract method compareTo(java.lang.Object) in java.lang.Comparable

however I have declared compareTo() at the 3 different classes in Person, Company and Invoice. What should I do to make my List class generic so that I am able to use the compareTo inside List insert() is able to call the appropriate class instead?? Thanks in advance

import java.lang.Comparable;
class List implements Comparable
{

......
public void insert(Object var)
{
ListNode newNode = new ListNode(var);
setNumItems(size()+1);
if(isEmpty())
setHead(newNode);
else
{ ListNode prev=null;
for(ListNode curr=getHead(); curr != null; prev = curr, curr=curr.getNext())
{
Object tmpItem = curr.getItem();
if(tmpItem.compareTo(var) > 0)
{
// insert before curr node
newNode.setNext (curr);
if (prev != null)
prev.setNext (newNode);
else
setHead (newNode);

return;
}
}
// insert at end of list
prev.setNext (newNode);
}
}
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18836
    
  40

Originally posted by Timothy Leong:

I get this error:
List is not abstract and does not override abstract method compareTo(java.lang.Object) in java.lang.Comparable

however I have declared compareTo() at the 3 different classes in Person, Company and Invoice. What should I do to make my List class generic so that I am able to use the compareTo inside List insert() is able to call the appropriate class instead??


The compiler is not complaining about Person, Company, or Invoice. It is complaining about List. You declare the List as implementing comparable, but it doesn't.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Timothy Leong
Ranch Hand

Joined: May 25, 2005
Posts: 55
Hi, I know i need to redefine compareTo for the Object in List. How to actually implement this compareTo??


if(tmpItem.compareTo(var) > 0)

is like Object.compareTo(Object)

for Customer we can have compareTo inside Customer class,
for Object, I really lost...

I am hoping to make use of Object.compareTo(Object), so that I can use on Customer,Invoice LinkList
[ September 23, 2005: Message edited by: Timothy Leong ]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18836
    
  40

Originally posted by Timothy Leong:
Hi, I know i need to redefine compareTo for the Object in List. How to actually implement this compareTo??

if(tmpItem.compareTo(var) > 0)

is like Object.compareTo(Object)

for Customer we can have compareTo inside Customer class,
for Object, I really lost...

I am hoping to make use of Object.compareTo(Object), so that I can use on Customer,Invoice LinkList


The standard Object class does not have a compareTo() method. You need to cast the object to what it originally is so that you can call the compareTo() method. Or better yet, if all your objects implement comparable, then cast it to Comparable.

Henry
Timothy Leong
Ranch Hand

Joined: May 25, 2005
Posts: 55
Thank Henry

I tried casting both variables using Comparable. The program works , however when i compile it, it produce a warning "..uses unchecked or unsafe operations" . What can i do so that the warning will never appear when i compile?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Don't use "Object" and cast things to Comparable. If you deal only in Comparable objects, then use Comparable everywhere, and get rid of the casts.


[Jess in Action][AskingGoodQuestions]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18836
    
  40

Originally posted by Ernest Friedman-Hill:
Don't use "Object" and cast things to Comparable. If you deal only in Comparable objects, then use Comparable everywhere, and get rid of the casts.


Good point. If you are going to endure the awkward syntax of Generics, you might as well let it type check for you.

Henry
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

You need to implement Comparable on the things you plan to compare, not on the list itself.

So Person, Company, and Invoice should implement Comparable. However, be warned that Comparable is not meant to compare different classes with each other. The list should have homogenious contents.

In any event a List is ordered so the objects will maintain the order you put them in.

If you want to compare these things you can put your list through a sorter like

You don't have to create a new linkedlist, its the elements you intend to put into the list that need the work. Why are you making your own linked list?
[ September 24, 2005: Message edited by: Mr. C Lamont Gilbert ]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Making your own List class is a great exercise to help learn about data structures. In fact, it is a common homework assignment in most Computer Science data structure classes.

However, it doesn't seem to me that anyone has addressed your original question about the compiler error. I see that you have "class List implements Comparable". This is probably what the compiler is complaining about since it expects your List class to have a compareTo() method. But do you really need to compare Lists with each other? What does it mean to compare one list with another? I doubt this is what you intend, so you should probably remove "implements Comparable" from this line of code.

I hope this answers your question. If not, please let us know what new problems you are encountering and we will continue to help you.

Regards,

Layne


Java API Documentation
The Java Tutorial
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Object Comparable