【问题标题】:How to call a method on an object in an arraylist如何调用数组列表中对象的方法
【发布时间】:2019-05-09 03:39:59
【问题描述】:

所以我有一个名为 Team 的类,用于创建 Team 对象。在另一个名为 LeagueAdmin 的类中,我创建了一个 map<String, List<Team> 类型的映射,正如您在我的代码中看到的那样,我将键设置为部门字符串,然后将团队对象分配给不同的部门。 我的问题是我试图创建一个方法,在一个部门中找到两个指定的团队,比较两个整数,如果 x 大于 y,则对每个对象调用某个方法。即如果A队的进球比B队多,则将与A队同名的对象的获胜值加1。

我的 LeageAdmin 代码是:

    import java.util.*;
    public class LeagueAdmin {

private Map<String, List<Team>> teams;

/**
 * Constructor for objects of class LeagueAdmin
 *
 */
public LeagueAdmin() {
    this.teams = new HashMap<>();
}
public void addTeam(String division, Team team) {
    if (!this.teams.containsKey(division)) {
        List<Team> teamList = new ArrayList<>();
        teamList.add(team);
        this.teams.put(division, teamList);
    } else {
        List<Team> newTeam = this.teams.get(division);
        newTeam.add(team);
        this.teams.put(division, newTeam);
    }
}
public void recordResult(String division, String teamA, String teamB, int teamAScore, int teamBScore)
{

    List<Team> teamList = teams.get(division);
    for(Team team : teamList) {
        if(teamA.equals(team.getName())) {
            teamA = String.valueOf(team);
        } else if (teamB.equals(team.getName())) {
            teamB = String.valueOf(team);
        }
    }
    if (teamAScore == teamBScore)
    {
        teams.get(division).get(teamA).incDrew();
        teams.get(division).get(teamB).incDrew();
    } else if (teamAScore > teamBScore) {
        teams.get(division).get(teamA).incWon();
        teams.get(division).get(teamB).incLost();
    } else {
        teams.get(division).get(teamA).incLost();
        teams.get(division).get(teamB).incWon();
    }
}

}

团队的代码是:

    public class Team
    {
        private String name;
        private String division;
        private int won;
        private int drew;
        private int lost;
        // no need to record points as = 3*won + drew


/**
 * Constructor for objects of class Team
 */
public Team(String aName, String aDivision)
{
    name = aName;
    division = aDivision;
    // no need to set won, drew and lost to 0
}


/**
 * getter for attribute points
 */
public int getPoints()
{
    return 3 * won + drew;
}

/**
 * getter for name
 */
public String getName()
{
    return name;
}

/**
 * getter for division
 */
public String getDivision()
{
    return division;
}
/**
 * getter for won
 */
public int getWon()
{
    return won;
}

/**
 * getter for drew
 */
public int getDrew()
{
    return drew;
}

/**
 * getter for lost
 */
public int getLost()
{
    return lost;
}

/**
 * increments the number of games won
 */
public void incWon()
{
    won = won + 1;
}

/**
 * increments the number of games drawn
 */
public void incDrew()
{
    drew = drew + 1;
}

/**
 * increments the number of games lost
 */
public void incLost()
{
    lost = lost + 1;
}

/**
 * setter for division
 */
public void setDivision(String aDivision)
{
    division = aDivision;
}

public String toString()
{
    return ("Team " + name + ", division: " + division + " stats: Won: " + won
            + ", drew: " + drew + ", lost: " + lost + ", points: " + getPoints());
}

}

正如您所见,方法 recordResult() 的代码是我遇到的问题,我从方法的参数中引用字符串,它似乎认为它应该接收一个整数?我的代码几乎是正确的还是有更好的方法?

public void recordResult(String division, String teamA, String teamB, int teamAScore, int teamBScore)
{
    teamA = null;
    teamB = null;

    List<Team> teamList = teams.get(division);
    for(Team team : teamList) {
        if(teamA.equals(team.getName())) {
            teamA = String.valueOf(team);
        } else if (teamB.equals(team.getName())) {
            teamB = String.valueOf(team);
        }
    }
    if (teamAScore == teamBScore)
    {
        teams.get(division).get(teamA).incDrew();
        teams.get(division).get(teamB).incDrew();
    } else if (teamAScore > teamBScore) {
        teams.get(division).get(teamA).incWon();
        teams.get(division).get(teamB).incLost();
    } else {
        teams.get(division).get(teamA).incLost();
        teams.get(division).get(teamB).incWon();
    }
}

【问题讨论】:

  • 您将teamA 设置为空。为什么?这不仅会在if(teamA.equals(team.getName())) 上抛出一个NPE,它还会丢弃teamA 的值
  • 我已经删除了它,我一定是从我尝试的不同解决方案中留下了它,但是没有那个仍然会出现同样的错误。
  • 你得到的确切错误是什么?
  • method get in interface java.util.List&lt;E&gt; cannot be applied to given types; required: int; found: java.lang.String;
  • teams.get(division) 返回一个List,然后调用.get(teamA),将String 传递给get(),它接受一个int 参数。查看indexOf

标签: java list methods hashmap


【解决方案1】:

你已经接近可用的东西了! recordResults() 看起来像是要修改 Team 对象的状态,但由于该方法接受团队名称而不是 Team 对象,因此您需要先进行查找。

您的for 循环正在通过适当部门中的Team 对象查找Team,其名称在函数参数teamAteamB 中提供。这很好,您只需将Team 对象存储在您的变量中而不是团队名称中,以便您稍后可以在方法中访问它。因此,您需要存储team,而不是存储String.valueOf(team)

等一下,你说。我不能将Team 对象放入String 变量中!你是对的。方法开头的teamAteamB 变量应声明为Team 类型,并给出不与方法的参数名称冲突的名称。就个人而言,我会重命名方法参数 teamANameteamBName,因为它们只包含团队名称,而不是整个 Team 对象。

现在您已经成功地通过名称查找了Team 对象(我们将把错误处理作为稍后的练习),您只需要调用它们相应的评分方法。因此,在您的 if-else 块检查团队得分时,您应该直接在我们从查找中保存的 teamAteamB 变量上调用您的 inc... 方法。

【讨论】:

    猜你喜欢
    • 2018-10-21
    • 2012-04-21
    • 1970-01-01
    • 2015-04-28
    • 1970-01-01
    • 1970-01-01
    • 2019-10-22
    • 1970-01-01
    • 2017-10-11
    相关资源
    最近更新 更多