Adam Duffy
Posts: 168
Nickname: adamduffy
Registered: Feb, 2003
|
|
Re: a problem about compareTo(Object rv)
|
Posted: Apr 12, 2005 6:39 AM
|
|
Code is
public int compareTo(Object rv) {
int rvi = ((CompType)rv).i;
int reInt=0;
return (i < rvi ? -1 : (i == rvi ? 0 : 1));
}
For sake of brevity, we'll leave out checks for null and type.
Questions: 1. Is the compareTo method contained within the CompType class? 2. What does the reInt variable do?
It looks like you're essentially comparing objects based on an int attribute. Don't make work for yourself. Just return the difference of the two.
return( i - rvi );
According to the Java Docs for the JDK, the compareTo method "returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object."
Adam
|
|