views:

70

answers:

3

My problem is i have a class and in it there is a list of elements of another class.

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 =new ArrayList<Player>(players);  }
}
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; }
}

My problem is to print the players of a Branch with their name,pos,salary,number. For this i tried this simply :

String p1,p2;
int a1,a2;
p1 = input.readLine();
p2 = input.readLine();
a1 = Integer.parseInt(input.readLine());
a2 = Integer.parseInt(input.readLine());
players[0].setName(p1);
players[0].setPos(p2);
players[0].setSalary(a1);
players[0].setNumber(a2);
ptmp.add(players[0]);

myBranch[0].setPlayers(ptmp);

System.out.println(myBranch[0].brName +  "  " + myBranch[0].getPlayers());

I wrote this just to try how to display. I created an array of Players, and Branches so they already defined. The problem is getPlayers() doesn't give me any result. What is the way to do this?

+1  A: 

When you attempt to println or concatenate an ArrayList (as you are doing when you do " " + myBranch[0].getPlayers(), the method ArrayList.toString() is invoked to return a String representation of the list's contents.

ArrayList.toString() will return a String containing the result of toString() of each element in the list (this behavior is actually defined in one of it's parent classes AbstractCollection.

Since your Player class does not override the toString() implementation provided by java.lang.Object, the output is likely something like packagename.Player@a8f123.

So, if you'd like to have meaningful output when you call ArrayList.toString() on a list of Player objects, you should override toString() in Player to provide the String representation of a Player you would like to have returned.

matt b
A: 

++ to what matt b says. Another way you could do it is to override the toString on Branch so that is cycles through the array list and returns the information on each Player (perhaps calling the toString that matt b recommends is defined on the Player class).

Chris Aldrich
A: 

Give the Player class a toString() methods as such:

@Override
public String toString() {
    return "Player [name=" + name + ", number=" + number + ", pos=" + pos
            + ", salary=" + salary + "]";
}
maximus