請檢查下面的代碼,
import java.util.Arrays;
import java.util.Comparator;
public class Team {
private String name;
private double batAvg;
private double slugAvg;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBatAvg() {
return batAvg;
}
public void setBatAvg(double batAvg) {
this.batAvg = batAvg;
}
public double getSlugAvg() {
return slugAvg;
}
public void setSlugAvg(double slugAvg) {
this.slugAvg = slugAvg;
}
public static void main(String[] argv){
Team[] teams = new Team[2]; //TODO, for testing..
Arrays.sort(teams, new TeamComparator()); //This line will sort the array teams
}
}
class TeamComparator implements Comparator<Team>{
@Override
public int compare(Team o1, Team o2) {
if (o1.getSlugAvg()==o2.getSlugAvg()){
return 0;
}
return o1.getSlugAvg()>o2.getSlugAvg()?-1:1;
}
}
*「我已經讀了很多,但我真的無法弄清楚。」 *是什麼讓你覺得什麼我們寫會突然幫你圖出來? –