1

它的工作原理和一切,但它看起来很可怕。我们只需要一个包含曲棍球运动员数据的文本文件来获取数据。然后我们获取这些数据并从中创建玩家。然后列出这些球员的名单。我们从来没有给每支球队的胜利数量,所以我几乎想知道这是否是你能做的最好的。

基本上,它想要完成的是找到获胜最多的团队。这是通过计算每个球员的获胜目标,并根据他们来自哪支球队来判断的,并为该球队计算。

我通过遍历所有玩家并找到唯一的团队名称来创建团队列表作为团队对象来实现这一点。

然后我浏览了球员名单,如果球员的球队与当前球队相同,那么他们就会为获胜进球得分。

然后,在另一个 for 循环中,找到这些目标数量最多的团队。

归还这支球队。

对于一个小任务,总共有四个 for 循环。看起来很恶心。

    /**
     * Returns the team with the most wins
     */
    public Team getTeamWithMostWins() {
        Team teamWithMostWins = new Team();
        List<Team> teams = new List<Team>();

        if (!players.isEmpty()) {

            // Compile the List of teams
            for (int i = 0; i < players.size(); i++) {
                if (!teams.contains(players.get(i).getTeam())) {
                    teams.add(new Team(players.get(i).getTeam()));
                }
            }

            // Set the wins for the teams
            for (int i = 0; i < players.size(); i++) {
                String team = players.get(i).getTeam();
                int winningGoals = players.get(i).getWinningGoals();

                // Go through the teams List to set the points
                for (int j = 0; j < teams.size(); j++) {
                    // If the current player's team is equal to the current team in the loop
                    if ((teams.get(j).getName()).equals(team)) {
                        teams.get(j).incrementWinsBy(winningGoals);
                    }
                }
            }

            int mostWins = teams.get(0).getWins();

            // Find the team with the most wins
            for (int i = 1; i < teams.size(); i++) {
                if (teams.get(i).getWins() > mostWins) {
                    teamWithMostWins = teams.get(i);
                }
            }

        }
        else {
            teamWithMostWins = null;
        }

        return teamWithMostWins;
    }
4

5 回答 5

2

正如 Jordan Denison 在评论中指出的那样,您可以使用 for-each 循环。请参阅下面的示例。

此外,目前您只会获得胜场数多于第一队的最后一支队伍。为了获得最多胜利的团队,您必须更新最多胜利:

int mostWins = teams.get(0).getWins();

// Find the team with the most wins
for(Team team : teams) {
    if (team.getWins() > mostWins) {
        teamWithMostWins = team;
        mostWins = team.getWins(); // <--- Update the most wins every time you find a team with more wins
    }
}

更新

此外,请考虑使用其他答案中显示的地图。

于 2012-10-08T21:32:05.863 回答
2

您可以使用地图来存储每支球队的胜利次数:

import java.util.Map;
import java.util.HashMap;

/**
 * Returns the team with the most wins
 */
public Team getTeamWithMostWins() {
    if (players.isEmpty()) {
        return null;
    }

    Map<Team, Integer> teamWins = new HashMap<String, Integer>();

    // Set the wins for the teams
    for (Player player : players) {
        Integer count = teamWins.get(player.getTeam());
        count = (count == null)? 0 : count;
        teamWins.set(player.getTeam(), count + player.getWinningGoals());
    }

    Team teamWithMostWins = null;
    Integer mostWins = 0;

    for (Map.Entry<Team, Integer> teamWins : map.entrySet()) {
        Team team = entry.getKey();
        Integer wins = entry.getValue();
        if (wins > mostWins) {
            mostWins = wins;
            teamWithMostWins = team;
        }
    }

    return teamWithMostWins;
}

为此,您必须将 hashCode() 和 equals() 方法添加到您的 Team 类。

于 2012-10-08T21:38:46.500 回答
1

您可以通过将其分解为具有有意义名称的较小函数来改进此代码。

除此之外,您使用 Java 的事实有点束缚您的手脚。在对高阶函数有更好支持的语言中,这种代码可以写得非常非常简洁。

因为你要求一个例子......这是你的代码到 Ruby 的粗略端口:

teams = players.map(&:team).uniq
best_team = teams.max_by { |t| players.select { |p| p.team == t }.map(&:winning_goals).reduce(0,&:+) } 
于 2012-10-08T21:40:33.410 回答
1
int                  max      = 0;
Team                 mostWins = null;
Map< Team, Integer > counters = new HashMap<>();
for( Player player : players )
{
    Integer counter = counters.get( player.getTeam());
    if( counter == null ) counter = player.getWinningGoals();
    else                  counter = player.getWinningGoals() + counter
    counters.put( player.getTeam(), counter );
    if( counter > max )
    { 
        max      = counter;
        mostWins = player.getTeam();
    }
}
return mostWins;
于 2012-10-08T21:37:35.573 回答
0

使用谷歌番石榴:

final Multiset<Team> teamWins = HashMultiset.create();
for (Player player : players) {
  teamWins.add(player.getTeam(), player.getWinningGoals());
}

Team teamWithMostWins =
  Collections.max(teamWins, new Comparator<Team>() {
    public int compareTo(Team team1, Team team2) {
        return teamWins.count(team1) - teamWins.count(team2);
    }
  });

int mostWins = teamWins.count(teamWithMostWins);

基本上,番石榴系列让您的计数部分变得更简单。没有编译这个,但它应该给你正确的想法。只需确保 Team 具有正确的 .hashCode 和 .equals 方法(或者您可以假设您不会在不同的对象实例中复制团队数据);

于 2012-10-08T22:52:44.467 回答