我和我的班级在一起,我的软件几乎完成了最后两件事后我将继续GUI开发。无论如何,这是我的代码:
public class Team
{
private String clubName;
private String preName;
private ArrayList<Branch> branches;
public Team(String clubName, String preName)
{
this.clubName = clubName;
this.preName = preName;
branches = new ArrayList<Branch>();
}
public Team() {
// TODO Auto-generated constructor stub
}
public String getClubName() { return clubName; }
public String getPreName() { return preName; }
public ArrayList<Branch> getBranches() { branches = new ArrayList<Branch>(branches);return branches; }
public void setClubName(String clubName) { this.clubName = clubName; }
public void setPreName(String preName) { this.preName = preName; }
public void setBranches(ArrayList<Branch> branches) { this.branches = new ArrayList<Branch>(branches); }
}
public class Branch
{
public ArrayList<Player> players = new ArrayList<Player>();
String brName;
public Branch() {}
public void setBr(String brName){this.brName = brName;}
public String getBr(){return brName;}
public ArrayList<Player> getPlayers() {players =new ArrayList<Player>(players); return players; }
public void setPlayers(ArrayList<Player> players) { this.players =new ArrayList<Player>(players); }
public String toString() {
return "Branches [" + brName + "]";}
}
public class Player
{
private String name;
private String pos;
private Integer salary;
private Integer number;
public Player(String name, String pos, Integer salary, Integer number)
{
this.name = name;
this.pos = pos;
this.salary = salary;
this.number = number;
}
public Player(){}
public String getName() { return name; }
public String getPos() { return pos; }
public Integer getSalary() { return salary; }
public Integer getNumber() { return number; }
public void setName(String name) { this.name = name; }
public void setPos(String pos) { this.pos = pos; }
public void setSalary(Integer salary) { this.salary = salary; }
public void setNumber(Integer number) { this.number = number; }
public String toString() {
return "Player [name=" + name + ", number=" + number + ", pos=" + pos
+ ", salary=" + salary + "]";
}
}
//TEST
String p1,p2;
int a1,a2;
String t, br;
System.out.print("Enter player name : ");
p1 = input.readLine();
System.out.print("Enter player position : ");
p2 = input.readLine();
System.out.print("Enter player salary : ");
a1 = Integer.parseInt(input.readLine());
System.out.print("Enter player number : ");
a2 = Integer.parseInt(input.readLine());
players[pCount].setName(p1);
players[pCount].setPos(p2);
players[pCount].setSalary(a1);
players[pCount].setNumber(a2);
ptmp.add(players[pCount]);
pCount++;
System.out.print("Enter the branch of player : ");
br = input.readLine();
int fff=0;
for(int i = 0; i<brCount;i++)
{
if(br.equals(myBranch[i].brName)==true){
myBranch[i].setPlayers(ptmp);fff=i;}
}
我的第一个问题:我正在尝试将播放器添加到我的系统中。添加一个播放器后我也可以轻松地将它添加到Branch类并连接它们。但我不能为球员俱乐部做这件事。我的意思是我想显示哪个球员在哪个俱乐部比赛。但我不能这样做。
我的第二个问题:删除玩家也是个问题。当我删除播放器时,它应该被删除。但是无法理解这一点。
在测试中,您可以看到我尝试过的显示功能。它适用于Branch-Player。我也想添加团队连接。应该连接Team-Branch-Player。
答案 0 :(得分:1)
Q1:这取决于你想要进行搜索的效率..现在,既然你没有存储参考文献,你必须首先搜索哪个分支是你的玩家,然后搜索包含你的分支的俱乐部
对于equals
和Branch
课程采用良好的Player
方法,这是微不足道的:
for (Team t : teamList)
{
if (t.branches.contains(player))
return true;
}
return false;
但这不会有效率,因为你的 O(n * m)复杂度,其中 n 是团队规模而 m 是平均分支大小。
如果你想要更高效的东西,我建议你在你的课程中存储反向引用,你可以让你的Player
课程有两个属性
Branch currentBranch
Team currentTeam
您可以在将玩家添加到分支/团队时设置它们。
否则你可以保留一个单独的HashMap
,将每个玩家映射到他的分支/团队。内存效率较低但非常简单。
Player
你只需要知道他留在哪一个..(使用Q1的答案),然后在从players
移除之前你只需删除来自相应的分支/团队:
Branch b = findWhichBranch(player);
Team t = findWhichTeam(player);
b.remove(player);
t.remove(player);
players[index] = null;
当然,如果团队隐含分支,您只需将其从分支中移除,因为玩家和团队之间没有直接关联。