在JAVA中安排课程内容的问题

时间:2010-06-09 11:05:58

标签: java arraylist

我有一些课程,我正在尝试填写这门课程的对象。这是我尝试过的。 (问题如下)

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; }
}

// TEST CLASS

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

    }

    }
}

}

问题是我的班级元素之一。我有一个arraylist作为一个类的元素。 当我进入:

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()方法,但引起了问题。任何想法。

2 个答案:

答案 0 :(得分:1)

问题在于两个Team个对象与单个ArrayList<String>共享相同的引用。有很多种方法可以解决这个问题,但有一种方法是让Team管理自己的List<Branch>,它应该只展示add(Branch)而不是setBranches(List<Branch>)。这会隐藏客户端的大部分信息,只暴露最基本的功能,这是一件好事。

另请注意,我使用界面List<Branch>代替ArrayList<Branch>(或ArrayList<String>)。这符合 Effective Java 2nd Edition,Item 52:通过接口引用对象


我还建议使用java.util.Scanner作为I / O.查看API的示例,关于它的stackoverflow也有很多问题。它使代码更简单。

答案 1 :(得分:0)

你需要在setter中复制列表,否则你在任何地方使用相同的列表(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); 
}

理论上,你还需要在getter中复制或包装它,但这是另一个故事。