3

作为一个大学项目,我正在编写一个 Java 程序,该程序采用派生自 Player 类的类并将它们存储在 Club 类中。俱乐部是一个板球俱乐部(因此变量/类名)。下面的类仍然只是部分构建,但它可以编译并且对于我需要解决的问题来说足够完整。编译以下类时,我收到两个“未经检查”的警告:

import java.util.*;

public class Club{
    private String name;
    private List<Player> players;
    private Set<Player> playersAverage;
    private int regID;

    @SuppressWarnings(value = {"unchecked"})
    public Club(){
        this.name = "";
        this.players = new ArrayList<Player>();
        this.playersAverage = new TreeSet<Player>(new BattingAverageComparator());
        this.regID = 1;
    }

    @SuppressWarnings(value = {"unchecked"})
    public Club(String name){
        this.name = name;
        this.players = new ArrayList<Player>();
        this.playersAverage = new TreeSet<Player>(new BattingAverageComparator());
        this.regID = 1;
    }

    public void setName(String name){
        this.name = name;
    }

    public String getName(){
        return this.name;
    }

    public boolean registerPlayer(Player player) throws UninitialisedObjectException, NullPointerException{
        if(!(validPlayer(player))){
            throw new UninitialisedObjectException("attempted to add an uninitialised player object to Club.players");
        }
        if(!(this.players.contains(player))){
            player.setRegID(this.regID);
            this.regID++;
            for(int i = 0; i < this.players.size(); i++){
                if(player.compareTo(this.players.get(i)) > 0){
                    this.players.add(i,player);
                    return true;
                }
            }
        }
        return false;
    }

    public boolean removePlayer(Player player) throws NullPointerException{
        return this.players.remove(player);
    }

    public String getPlayerDetails(int regID) throws InvalidRegistrationIDException{
        String s = "";
        for (int i=0; i < this.players.size(); i++){
            if (this.players.get(i).getRegID() == regID){
                s = this.players.get(i).toString();
                break;
            }
        }
        if(s == ""){
            throw new InvalidRegistrationIDException("getPlayerDetails() attempted on invalid regID");
        }
        return s;
    }

    private boolean validPlayer(Player player){
        return player.getFirstName()!="" || player.getLastName()!="" || player.getAge()>0 || player.getHeight()>0 || player.getWeight()>0;
    }

    public void averages(BattingAverageComparator compareAveragesOf){
    }
}

使用以下比较器:

import java.util.*;

public class BattingAverageComparator implements Comparator{
    public int compare(Object obj1,Object obj2) throws IllegalArgumentException{
        if(!(obj1 instanceof Player) || !(obj2 instanceof Player)){
            throw new IllegalArgumentException("BattingAverageComparator cannot compare objects that are not of, or do not extend, the Player class.");
        }
        Player thisPlayer = (Player) obj1;
        Player thatPlayer = (Player) obj2;
        if(thisPlayer.getDismissals() == 0 && thatPlayer.getDismissals() == 0){
            if(thisPlayer.getRuns() > thatPlayer.getRuns()){
                return 1;
            }
            else if (thisPlayer.getRuns() < thatPlayer.getRuns()){
                return -1;
            }
            else{
                return thisPlayer.compareTo(thatPlayer);
            }
        }
        else if(thisPlayer.getDismissals() == 0 && thatPlayer.getDismissals() > 0){
            return -1;
        }
        else if(thisPlayer.getDismissals() > 0 && thatPlayer.getDismissals() == 0){
            return 1;
        }
        else{
            double thisAverage = thisPlayer.getRuns()/thisPlayer.getDismissals();
            double thatAverage = thatPlayer.getRuns()/thatPlayer.getDismissals();
            if(thisAverage > thatAverage){
                return 1;
            }
            else if(thisAverage == thatAverage){//need to make a double threshold
                return 0;
            }
            else{
                return -1;
            }
        }
    }

    public boolean equals(Object obj){
        return obj instanceof BattingAverageComparator;
    }
}

两个构造函数都会出现以下警告:

Club.java:13: warning: [unchecked] unchecked conversion found : BattingAverageComparator
required: java.util.Comparator<? super Player>
this.playersAverage = new TreeSet<Player>(new BattingAverageComparator());

除了抑制警告之外,还有什么方法可以解决这个问题吗?

如果您需要更多信息,我会发布它。该程序中有很多课程,我认为目前没有必要将它们全部发布。

4

2 回答 2

7

问题在这里:

public class BattingAverageComparator implements Comparator{

您将其声明为原始比较器,但您输入的是通用类型<Player>

所以改成

public class BattingAverageComparator implements Comparator<Player>{
于 2012-04-21T21:10:33.297 回答
1

是的,使用泛型类型:

public class BattingAverageComparator implements Comparator<Player>{
  public int compare(Player obj1,Player obj2){
    //etc.
  }
}
于 2012-04-21T21:08:50.000 回答