PriorityQueue<Player> playerQueue = new PriorityQueue<Player>(30,
new Comparator<Player>( ) {
// overriding the compare method
public int compare(Player i, Player j) {
return (int)i.playerPrice() < (int)j.playerPrice() ? -1 : ((int)i.playerPrice() == (int)j.playerPrice()) ? 0 : 1;
}
}
);
for(Player a:XMLParserViaSax.getListOfPlayers()){
System.out.println(a.getName()+ "-->"+a.playerPrice());
}
for(Player player:XMLParserViaSax.getListOfPlayers()){
playerQueue.add(player);
}
//playerQueue.addAll(XMLParserViaSax.getListOfPlayers());
System.out.println("**************************");
for(Player j:playerQueue){
System.out.println(j.getName()+ "-->"+j.playerPrice());
}
我正在尝试按 playerprice 对 Player 进行排序,因为 Player 类中的 playerprice 没有任何变量。playerPrice() 是一种计算玩家价格的方法。所以现在当我尝试对这些 Player 对象进行排序时,它是给我一个随机的顺序。
我的玩家类中是否需要有一个 playerprice 私人成员?' 播放器类是这样的:
public class Player {
private String name;
private int battingStrength;
private int bowlingStrength;
private int fieldingStrength;
private int keepingStrength;
private int jerseyNumber;
public int playerStrength() {
return AuctionPlayerHelper.calculateStrength(this);
}
public boolean canKeepWickets() {
return (keepingStrength == 10);
}
public boolean isForeignPlayer(){
return AuctionPlayerHelper.isForeignPlayer(this.jerseyNumber);
}
public double playerPrice() {
return AuctionPlayerHelper.calculatePrice(this);
}
//getters,setters
}
'