The Artima Developer Community
Sponsored Link

Java Answers Forum
a problem about compareTo(Object rv)

2 replies on 1 page. Most recent reply: Apr 12, 2005 6:39 AM by Adam Duffy

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Martin

Posts: 3
Nickname: martinren
Registered: Apr, 2005

a problem about compareTo(Object rv) Posted: Apr 11, 2005 8:31 PM
Reply to this message Reply
Advertisement
could rvi not equal i?

public int compareTo(Object rv) {
int rvi = ((CompType)rv).i;
int reInt=0;
return (i < rvi ? -1 : (i == rvi ? 0 : 1));
}
I think "int rvi = ((CompType)rv).i;" make rvi=i ,right?


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: a problem about compareTo(Object rv) Posted: Apr 11, 2005 10:30 PM
Reply to this message Reply
Yes, this method will allways return 0.

Adam Duffy

Posts: 168
Nickname: adamduffy
Registered: Feb, 2003

Re: a problem about compareTo(Object rv) Posted: Apr 12, 2005 6:39 AM
Reply to this message Reply
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

Flat View: This topic has 2 replies on 1 page
Topic: Convert Excel Applications to Java or .Net Previous Topic   Next Topic Topic: <thing in java 2nd>-Filling an array

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use