2010-06-09 32 views
1

我有一些類,我試圖填充這個類的對象。這是我試過的。 (問題是在下面)在JAVA中安排類的內容時的問題

public class Team 
{ 
    private String clubName; 
    private String preName; 
    private ArrayList<String> branches; 

    public Team(String clubName, String preName) 
    { 
     this.clubName = clubName; 
     this.preName = preName; 
     branches = new ArrayList<String>(); 
    } 

    public Team() { 
     // TODO Auto-generated constructor stub 
    } 

    public String getClubName() { return clubName; } 
    public String getPreName() { return preName; } 
    public ArrayList<String> getBranches() { return branches; } 

    public void setClubName(String clubName) { this.clubName = clubName; } 
    public void setPreName(String preName) { this.preName = preName; } 
    public void setBranches(ArrayList<String> branches) { this.branches = branches; } 
} 

public class Branch 
{ 
    private 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() { return players; } 
    public void setPlayers(ArrayList<Player> players) { this.players = players; } 
} 

//測試類

public class test { 

/** 
* @param args 
* @throws IOException 
*/ 
public static void main(String[] args) throws IOException { 

    String a,b,c; 
    String q = "q"; 
    int brCount = 0, tCount = 0; 
    BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); 
    Team[] teams = new Team[30]; 
    Branch[] myBranch = new Branch[30]; 
    for(int z = 0 ; z <30 ;z++) 
    { 
     teams[z] = new Team(); 
     myBranch[z] = new Branch(); 
    } 
    ArrayList<String> tmp = new ArrayList<String>(); 
    int k = 0; 
    int secim = Integer.parseInt(input.readLine()); 
    while(secim != 0) 
    { 
     if(k!=0) 
     secim = Integer.parseInt(input.readLine()); 
    k++;  
    switch(secim) 
    { 
    case 1 : 
     brCount = 0; 
    a = input.readLine(); 
    teams[tCount].setClubName(a); 
    b= input.readLine(); 
    teams[tCount].setPreName(b); 
    c = input.readLine(); 

    while(c.equals(q) == false) 
    { 
     if(brCount != 0) 
      {c = input.readLine();} 
     if(c.equals(q)== false){ 
     myBranch[brCount].brName = c; 
     tmp.add(myBranch[brCount].brName); 
     brCount++; 
     } 
     System.out.println(brCount); 
    } 

    teams[tCount].setBranches(tmp); 

    for(int i=0;i<=tCount;i++){ 
    System.out.print("a :" + teams[i].getClubName()+ " " + teams[i].getPreName()+ " "); 

    System.out.println(teams[i].getBranches());} 
    tCount++; 
    break; 
    case 2: 
     String src = input.readLine();//LATERRRRRRRr 

    } 

    } 
} 

}

問題是我的類元素之一。我有一個數組列表作爲一個類的元素。 當我輸入:

AAA as preName 
BBB as clubName 
c 
d 
e as Branches 

然後,作爲第二元件

www as preName 
GGG as clubName 
a 
b as branches 

The result is coming like: 
AAA BBB c,d,e,a,b 
GGG www c,d,e,a,b 

這意味着類的ArrayList的一部分把它和。我試圖使用clear()方法,但造成了問題。有任何想法嗎。

回答

0

你需要制定者複製列表,否則,您使用的是相同的列表(TMP)無處不在,所以難怪它具有相同的內容:

public void setBranches(List<String> branches) { 
    this.branches = new ArrayList<String>(branches); 
} 
public void setPlayers(List<Player> players) { 
    this.players = new ArrayList<Player>(players); 
} 

理論上,你還需要複製或者把它包在獲得者中,但這是另一回事。

+0

確切地:) :)。非常感謝 – LuckySlevin 2010-06-09 11:18:49

1

問題是,兩個Team對象共享相同的引用單個ArrayList<String>。有很多方法可以解決這個問題,但一種方法是讓Team管理它自己的List<Branch>,它應該只暴露add(Branch)而不是setBranches(List<Branch>)。這會隱藏客戶的大部分信息,只揭示最重要的功能,這是一件好事。

還請注意,我使用接口List<Branch>而不是ArrayList<Branch>(或ArrayList<String>)。這符合Effective Java第2版,第52項:通過其接口來引用對象。


我也推薦使用java.util.Scanner作爲I/O。看看API的例子,關於它的stackoverflow也有很多問題。它會使代碼更簡單。