• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

compare

 
yael buc
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello

i've got this error:
<anonymous strategies.BasicRational$1> is not abstract and does not override abstract method compare(java.lang.Object,java.lang.Object) in java.util.Comparator

for this code:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

...................
...................

Collections.sort(foodsRef,new Comparator(){
@Override
public int compare(Food f1,Food f2) {
Double value1 = f1.getGain() / (position.distance(f1.getPosition()) + f1.getWeight());
Double value2 = f2.getGain() / (position.distance(f2.getPosition()) + f2.getWeight());
return value1.compareTo(value2);
}
}
);

return foodsRef.get(0);
}

}

can somebody explain me the problem?
thanks

yael
 
Paul Clapham
Sheriff
Posts: 21111
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"yaeli bb", please check your private messages regarding an important administrative matter.

Thank you.
 
Jason Cone
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your compare method needs to specify Object arguments, rather than Food arguments.
 
yael buc
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry that i didn't mention it:

import game.Food;

food is a class in my code, therefor it's an object.. am i wrong?
 
Alex Hurtt
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are doing an overload, not an override. Important difference.

Also you can satisfy the compiler by using generics (assuming you are at least Java 5 or newer):


Now you're happy, compiler's happy...we're all one big happy ball of warm fuzziness.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic