它的作用和一切,但它看起来很可怕。我们所有用来绘制数据的是一个文本文件,其中包含曲棍球运动员的数据。然后我们获取这些数据并从中创建玩家。然后列出这些球员的名单。在任何时候我们都没有给出每个团队的胜利数量,所以我几乎想知道这个是否是你能做到的最好的。
基本上,它想要完成的是找到获胜最多的团队。这可以通过每个玩家的总成绩获得目标来确定,并根据他们来自哪个团队来判断,为该团队计算。
我通过遍历所有玩家并找到独特的团队名称来创建团队列表作为团队对象来完成此任务。
然后我浏览了玩家列表,如果玩家的团队与当前正在观察的团队相同,那么它将为获胜目标提供积分。
然后,在另一个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;
}
答案 0 :(得分: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
}
}
此外,请考虑使用其他答案中显示的地图。
答案 1 :(得分: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类。
答案 2 :(得分: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;
答案 3 :(得分: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,&:+) }
答案 4 :(得分:0)
使用google guava:
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方法(或者您可以假设您不会在不同的对象实例中复制团队数据);