• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Object Comparable

 
Timothy Leong
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21446
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Timothy Leong
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21446
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Henry Wong
author
Marshal
Pie
Posts: 21446
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic