views:

63

answers:

2

Hi, I have some classes and I'm trying to fill the objects of this class. Here is what i've tried. (Question is at the below)

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

    }

    }
}

}

The problem is one of my class elements. I have an arraylist as an element of a class. When i enter:

AAA as preName
BBB as clubName
c
d
e   as Branches

Then as a second element

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

Which means ArrayList part of the class is putting it on and on. I tried to use clear() method but caused problems. Any ideas.

A: 

You need to copy lists in setters, otherwise you are using the same list (tmp) everywhere, so no wonder it has the same contents:

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

Theoretically, you also need to copy or wrap it in getters, but that's another story.

unbeli
Exactly :):). Thanks so much
LuckySlevin
+1  A: 

The problem is that the two Team objects are sharing the same reference to a single ArrayList<String>. There are many ways to solve this, but one way is to let Team manage its own List<Branch>, and it should only expose an add(Branch) instead of setBranches(List<Branch>). This would hide most information from the client, exposing only the most essential functionalities, which is a good thing.

Note also that I use the interface List<Branch> instead of ArrayList<Branch> (or ArrayList<String>). This is in accordance with Effective Java 2nd Edition, Item 52: Refer to objects by their interfaces.


I also recommend using java.util.Scanner for the I/O. Look at the API for examples, and there are many questions on stackoverflow about it as well. It'd make the code much simpler.

polygenelubricants