Generic LinkedList using Comparable...? - can't afford extra points, but important! please help!!

I'm trying to write a LinkedList class which is generic (ie: uses type Object), but one of my methods has a compareTo() call (from Comparable)...when I compile, it tells me that (naturally) there is no compareTo() method in type Object.  How would I go about this?  Where would I add Comparable and implement compareTo()??

Here is my code for the list/nodes:

public class LinkedList {
     private Node first = new Node();
     private Node current = first;
     private Node pre = null;
     public boolean isEmpty() { return (first.getNext() == null); }

     public boolean inList(Object obj) {
          current = first;
          pre = null;
          while((current != null) && current.getData().compareTo(obj) > 0) {
               pre = current;
               current = current.getNext();
          return (current != null) && (current.getData().compareTo(obj) == 0);
     public Object peekAt() { return current.getData(); }
     public void insert(Object obj) {
          Node temp = new Node(obj);
          if(pre == null) { first = temp; }
          else { pre.setNext(temp); }
          current = temp;
     public void remove(Object obj) throws RunTimeException {
          if(!inList(obj)) { throw new RunTimeException("Item not found"); }
          else {
               current = current.getNext();
               if(pre == null) { first = current; }
               else { pre.setNext(current); }
     public void delete() {
          current = current.getNext();
          if(pre == null) { first = current; }
          else { pre.setNext(current); }
     public void traverse() {
          Node temp = first;
          while(temp != null) { System.out.prinln(temp.getData().toString()); }
          temp = temp.getNext();
     public void clear() {
          first = new Node();
          current = first;
          pre = null;

public class Node {
     private Object data;
     private Node next;

     public Node() { next = null; }

     public Node(Object obj) { data = obj; next = null; }

     public Node(Object obj, Node node) { data = obj; next = node; }
     public void setData(Object obj) { data = obj; }

     public void setNext(Node node) { next = node; }

     public Object getData() { return data; }

     public Node getNext() { return next; }
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You can't really, if you want to use compareTo() you should only allow Comparable objects to be added to your list. You could instead provide a Comparator to your list and it could use that instead of compareTo().

And looking at your code, I'm not sure I understand what you are using it for. The inList() method seems to require that the data in the list is sorted, which it may not be.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
grivadAuthor Commented:
Thanks, I already figured that out (using Comparable instead).. I just wanted to use Object to ensure genericity..

The idea of inList is that it will be called before every insert call, thus maintaining order.. now if somehow insert got called without calling inList first, yeah, there would be no order..

Thanks for your help!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.