• Post Reply Bookmark Topic Watch Topic
  • New Topic

a little stuck  RSS feed

 
joew weakers
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there. When I run the following code there is no problem whatsoever

Vector relevant_features = new Vector();
List sorted = Arrays.asList (ReturnBestContent.current_session_features.toArray());
/**sort the list of objects with current score used as the comparator*/
Collections.sort(sorted, new Comparator()
{
public int compare(Object left, Object right){
/**order is determined by left, right. Right, left gives descending order.*/
return((InterestFeature) right).getCurFeatScore() -
((InterestFeature) left).getCurFeatScore();
}
});

However, when I run this code I get an error where sobjs is an array of SessionObject objects:

List sorted = Arrays.asList(sobjs);
/**sort the list of objects with relevance score used as the comparator*/
Collections.sort(sorted, new Comparator()
{
public double compare(Object left, Object right){
return((SessionObject) right).getRelevanceScore() -
((SessionObject) left).getRelevanceScore();
}
});

The method getRelevanceScore returns a double. The error I am getting is as follows:

Error(209,5): method compare(java.lang.Object, java.lang.Object) in class getting_context.ReturnBestContent.1 cannot implement method compare(java.lang.Object, java.lang.Object) in interface java.util.Comparator with different return type, was int.

Anybody see what is wrong here. Thnaks a lot, Joe
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error message seems awfully clear: you've written compare() to return double, but the method you're overriding returns int. You can't change the return type when you override a method.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!