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
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.
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.
Joined: May 25, 2005
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?
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 ]
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.