3 Replies Latest reply: Oct 9, 2008 5:59 PM by 843785 RSS

    compareTo?

    843785
      I've been trying to get this compareTo thing to work, but I have a very small amount of knowledge on it, and I just can't seem to get it to work. Any suggestions? (becides going to find something on the internet, i've had bad luck with that)

      here is what I have but I really don't know how it is supposed to look...
      I beleive I am trying to find out if acar is bigger than bcar and whatnot...
          public int compareTo(Object acar, Object bcar)
          {
              int thecar = acar.compareTo(bcar);
              
              if (thecar > 0)
              {
                  return -1;
              }
              else if (thecar < 0)
              {
                  return 1;
              }
              else
              {
                  return 0;
              }
      thanks in advance.
        • 1. Re: compareTo?
          843785
          The compareTo method should only take a single parameter, the other object you want to compare this object to. Next, you will get a compiler error since your parameters are objects and you are trying to call the compareTo method on the first line but Object does not have a compareTo method. This what you are trying to write so get rid of that line completely. So if you want to compare 2 objects how would you do it? For example if you want to know if this object is bigger than that obejct how would you find that out?
          • 2. Re: compareTo?
            843785
            Suggestion: When you override a method, use attribute Override to verify this:
            public class X implements Comparable<X> {
                @Override public int compareTo(X that) {
                    ...
                }
            }
            • 3. Re: compareTo?
              843785
              Thanks everyone, I think I figured it out.