我必須按照誰贏得最多遊戲的順序來排列陣列「名稱」(下面)中的球隊。如果兩支球隊贏得相同數量的比賽,那麼我必須比較擊敗他們的球隊的勝利。我到目前爲止的代碼如下。如何遞歸使用比較器?
完整的問題陳述在這裏給出:http://www.cs.duke.edu/csed/newapt/tournamentrank.html
所以我想用遞歸比較。比較器如何訪問原始數據?我嘗試創建一個Team類,它爲團隊打敗了同一類的變量,但這顯然不起作用。困在這裏,請幫忙!
public class TournamentRanker implements Comparator<String>{
public class Team {
String name;
Integer wins;
Team beatEm;
}
//HOW TO make maps visible to comparator?
public String[] rankTeams(String[] names, String[] lostTo) {
//map all teams to number of wins & to team that beat them
ArrayList<String> teams = new ArrayList<String>();
HashMap<String, Integer> Teamwins = new HashMap<String, Integer>();
HashMap<String, String> Whobeat = new HashMap<String, String>();
for(int x=0; x<names.length; x++)
{
if(!teams.contains(names[x]))
teams.add(names[x]);
if(!Teamwins.containsKey(names[x]))
Teamwins.put(names[x], 0);
Whobeat.put(names[x], lostTo[x]);
if(!Teamwins.containsKey(lostTo[x]) && !lostTo[x].equals(""))
Teamwins.put(lostTo[x], 0);
if(!lostTo[x].equals(""))
Teamwins.put(lostTo[x], (Teamwins.get(lostTo[x])+1));
}
for(String s: names)
{
Integer wins = Teamwins.get(s);
Team beatEm = new Team(Whobeat.get(s), Teamwins.get(Whobeat.get(s)), ????)
}
//SORT list & turn into ARRAY
Comparator<String> comp = new TournamentRanker();
Collections.sort(teams, comp);
String [] sortedTeams = new String[teams.size()];
return teams.toArray(sortedTeams);
}
//NEED to use compareTo***?? OTHER strategy????
//USE COMPARTOR - how to access all the data?
public int compare(String team1, String team2){
}
}