【问题标题】:Avoiding adding duplicate objects to an ArrayList避免向 ArrayList 添加重复对象
【发布时间】:2017-02-08 03:09:24
【问题描述】:

这是要点,基本上我有这个方法,除了一个我似乎无法获得的条件。条件是如果addGame() 使用相同的两个字符串被调用两次,它不会将Game 对象存储在games ArrayList 中,因为它将返回false。我已经尝试使用 ArrayList contains() 方法来修复它,但是我创建的 JUnit 测试每次都失败。这是该方法的代码:

public class Conference {


    private ArrayList<Team> teams;
    private ArrayList<Player> players;
    private ArrayList<Game> games;
    public Conference(){
        teams = new ArrayList<Team>();
        players = new ArrayList<Player>();
        games = new ArrayList<Game>();
    }
public boolean addGame(String team1, String team2) {
      Game tempgame = new Game(team1, team2, 0, 0);
      Team first = new Team(team1, 0, 0, 0);
      Team second = new Team(team2, 0, 0, 0);
      if(!tempgame.getFirst().equals(tempgame.getSecond())){
          games.add(tempgame);
          first.addGamesPlayed();
          second.addGamesPlayed();
          teams.add(first);
          teams.add(second);
       return true;
      }
      return false;
}

Game 类如下:

package conference;
import java.util.ArrayList;

public class Game {
    private String firstTeam;
    private String secondTeam;
    private int firstTeamGoals;
    private int secondTeamGoals;
    private ArrayList<Team> team;
    public Game(String first, String second, int goals1, int goals2){
        this.firstTeam = first;
        this.secondTeam = second;
        this.firstTeamGoals = goals1;
        this.secondTeamGoals = goals2;
        team = new ArrayList<Team>();
    }
    public String getFirst(){
        return new String(firstTeam);
    }
    public String getSecond(){
        return new String(secondTeam);
    }
    public int getFirstTeamGoals(){
        return this.firstTeamGoals;
    }
    public int addFirstTeamGoals(){
        return firstTeamGoals++;
    }
    public int getSecondTeamGoals(){
        return this.secondTeamGoals;
    }
    public int addSecondTeamGoals(){
        return secondTeamGoals++;
    }
    public boolean hasMatchup(String t1, String t2){
        if(this.firstTeam.equals(t1) && this.secondTeam.equals(t2)){
            return true;
        }
        if(this.firstTeam.equals(t2) && this.secondTeam.equals(t1)){
            return true;
        }
        return false;
    }
}

还有Team 类:

package conference;

public class Team {


    private String teamName;
    private int goalsScored;
    private int gamesPlayed;
    private int gamesWon;
    public Team(String name, int totalGoals, int games, int wins){
    this.teamName = name;
    this.goalsScored = totalGoals;
    this.gamesPlayed = games;
    this.gamesWon = wins;
    }

    public String getName(){
        return new String(teamName);
    }
    public int getTotalGoals(){
        return goalsScored;
    }
    public int addGoals(){
        return goalsScored++;
    }
    public int addGamesPlayed(){
        return this.gamesPlayed++;
    }
    public int getGamesPlayed(){
        return gamesPlayed;
    }
    public int addGamesWon(){
        return gamesWon++;
    }
    public int getGamesWon(){
        return gamesWon;
    }
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        } else if (obj == this) {
            return true;
        } else {
            if (!(obj instanceof Team)) {
                return false;
            } else {
                Team temp = (Team) obj;
                return this.teamName.equals(temp.getName());
            }
        }
    }
}

【问题讨论】:

  • 是不能永远存储相同的两个字符串还是不能连续存储相同的两个字符串?
  • 永远无法存储相同的两个字符串
  • 字符串存储在游戏对象中,这就是为什么我不能将两个相同的对象存储在 ArrayList 中

标签: java class arraylist duplicates


【解决方案1】:

您的方法可能会失败,因为尽管字符串不相等,但这并不意味着 teamsgames 列表没有已经这些字符串。您需要遍历游戏列表并检查名称是否等于team1team2,如果是,则返回false。

你比较给定的团队不一样很好,你可以使用你的参数来比较,但这不是你需要的唯一条件。

if(!team1.equals(team2)) {

此外,您在已经是字符串的值上拥有 new String 的事实会在内存中创建一个新对象(因此它没有意义),但 equals 方法应该仍然有效。

【讨论】:

  • 还应注意这是为了防止相同的配对
  • 对。这很好,你只是没有检查gamesteams是否已经有team1team2
【解决方案2】:

“字符串存储在游戏对象中,这就是为什么我不能将两个相同的对象存储在 ArrayList 中”(您的评论。)

似乎为了换取更多内存,您可以使用SetHashSetTreeSet)来存储以前玩过的游戏的记录。如果您要防止相同的配对而不是相同的团队名称(在任何时候),请始终对两个 String 团队名称进行排序并将它们连接成一个键。检查该键是否已存在于集合中。

编辑:仅查看 ArrayList 的其他解决方案。

【讨论】:

  • 啊,忘了说一件事,另一个限制是我只能使用 ArrayList、Math 和 String 类。
  • @yarcenahs snap 嗯,我就是这样做的。 :) 我想这可能是个人的“额外功劳”。
猜你喜欢
  • 1970-01-01
  • 2020-09-23
  • 2016-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-29
  • 2015-07-30
  • 2016-04-17
相关资源
最近更新 更多