2010-06-09 197 views
1

我在這裏與我的課程,我的軟件差不多完成了後兩件事,我將繼續進行GUI開發。總之,這裏是我的代碼:刪除內容並顯示JAVA中的所有內容

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類並連接它們。但是我不能爲球員俱樂部做。我的意思是我想顯示哪個球員在哪個俱樂部中踢球。但我做不到。

我的第二個問題:刪除玩家也是問題。當我刪除播放器時,應該將其全部刪除。但無法想象出來。

在測試中,您可以看到我嘗試過的顯示功能。它適用於分支播放器。我也想添加團隊連接。團隊分支機構應該連接。

回答

1

問題1:這取決於您想如何有效地進行搜索..現在,由於您不存儲引用,您必須首先搜索哪個分支是您的玩家,然後搜索哪個俱樂部包含您的科。

具有良好的equals方法爲您BranchPlayer類,這是微不足道的:

for (Team t : teamList) 
{ 
    if (t.branches.contains(player)) 
    return true; 
} 

return false; 

但是這不會是有效的,因爲你將有一個O(N * M)複雜,其中ñ是團隊規模和是平均分支規模。

如果你想要的東西更有效,我建議您存儲類中的反向引用,你可以有你Player類兩個屬性

Branch currentBranch 
Team currentTeam 

,你可以設置他們,而你的播放器添加到分支/隊。

否則,您可以保留一個單獨的HashMap,將每個玩家都映射到他的分支/團隊。內存使用效率較低,但非常簡單。第二季度:要從他的分支/團隊中刪除Player,您只需知道他留在哪一個中(使用第一季度的答案),然後在從players中刪除之前,您只需將其從相應的分支/團隊中刪除:

Branch b = findWhichBranch(player); 
Team t = findWhichTeam(player); 

b.remove(player); 
t.remove(player); 

players[index] = null; 

當然,如果分支是由團隊暗示你只是從分支刪除它,因爲有球員和球隊之間沒有直接關聯。