gpt4 book ai didi

java - 如何调用数组列表中对象的方法

转载 作者:行者123 更新时间:2023-12-02 05:50:39 24 4
gpt4 key购买 nike

所以我有一个名为 Team 的类,用于创建 Team 对象。在另一个名为 LeagueAdmin 的类中,我创建了一个 map<String, List<Team> 类型的 map 。正如您在我的代码中看到的,我将键设置为部门字符串,然后将团队对象分配给不同的部门。我的问题是我试图创建一个方法,在一个部门中找到两个指定的团队,比较两个整数,如果 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();
}
}

最佳答案

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

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

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

既然您已经成功地通过名称查找了 Team 对象(我们将把错误处理留作稍后的练习),您只需调用它们适当的评分方法即可。因此,在检查团队分数的 if-else block 中,您应该直接在 teamA 上调用 inc... 方法我们从查找中保存的 teamB 变量。

关于java - 如何调用数组列表中对象的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56051828/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com