0

Tody i have tried to code a program, with administrate virtual Footballplayers. Now i have the problem, that i dont know how to initialize an Array of another Class in the Constructer.

This are my declarations for my "Teams"

Manager managerOfBayern = new Manager("Manager1",55,125);
Manager managerOfBvb = new Manager("Manager2",60,122);
Team fcBayern = new Team(managerOfBayern,playerArrFcB,"FcBayern");
Team bvb = new Team(managerOfBvb,playerArrBvb,"Bvb");

Now i want to initialize my Team.

public class Team {

Manager theManager;
Player[] thePlayer;
String name;


public Team(Manager theManager, Player[] thePlayer, String name) {
    this.theManager = theManager;
    for (int i = 0; i < thePlayer.length; i++) {
        thePlayer[i] = thePlayer[i];
    }
    this.name = name;
}

But how can i correctly initialize an Array (thePlayer)

i hope you guys can help me with this problem.....

Amerousful
  • 827
  • 8
  • 18

2 Answers2

1

What you are doing is almost correct!
just in the the part of array initialization that is the for loop that you are running in the constructor for copying the array, just replace left hand side of the operator '=' with 'this.thePlayer[i]' and you also need to specify the size of the array beforehand to initialize and use it in the for loop i.e the resultant constructor code should be like this

public Team(Manager theManager, Player[] thePlayer, String name) {
    this.theManager = theManager;
    this.thePlayer = new Player[thePlayer.length];
    for (int i = 0; i < thePlayer.length; i++) {
        this.thePlayer[i] = thePlayer[i];
        //or this.thePlayer[i] = new Player(thePlayer[i]); in case you want true deep copy, then in Player class you make a constructor of this signature(also known as copy constructor) and copy all the properties of Object passed as an argument  
    }
    this.name = name;
}
mss
  • 1,228
  • 1
  • 7
  • 16
0

I don't have the right solution for your problem but I can think for another way if it's good for you. check it out :

For Player class :

private String name;
private int age;
private int height;

public Player(String name, int Age , int height){
    this.name=name;
    this.age= Age;
    this.height = height;
}

public String getName(){
    return this.name;
}

public int getAge(){
    return this.age;
}

public int getHeight(){
    return this.height;
}

@Override
public String toString(){
    return this.name +" , "+ this.age +" , "+ this.height;
}

For Manager class :

private String name;

public Manager(String name){
    this.name = name;
}

public String getManager(){
    return this.name;
}

For Manager class :

Manager theManager;
Player[] thePlayer;
String name;

public Team(Manager theManager, Player[] thePlayer , String name){
    this.theManager = theManager;
    this.thePlayer = thePlayer;
    this.name = name;
}


@Override
public String toString(){
    String list = null ;
    for (int i = 0;i<thePlayer.length;i++){
        list += thePlayer[i].toString()+"\n";
    }
    return "Team name : " + this.name + "\n Team manager : " + this.theManager.getManager() + "\n Team players : \n" + list;
}

For team class :

Manager theManager;
Player[] thePlayer;
String name;

public Team(Manager theManager, Player[] thePlayer , String name){
    this.theManager = theManager;
    this.thePlayer = thePlayer;
    this.name = name;
}


@Override
public String toString(){
    String list = null ;
    for (int i = 0;i<thePlayer.length;i++){
        list += thePlayer[i].toString()+"\n";
    }
    return "Team name : " + this.name + "\n Team manager : " + this.theManager.getManager() + "\n Team players : \n" + list;
}

and finally the test class , so if you noticed you don't really need to put that loop inside the team constructor for player :

For test class(main) :

  Manager A = new Manager("Flic");

  Player P1 = new Player("Robert Levandowski",32,182);
  Player P2 = new Player("David Alaba",32,179);
  Player P3 = new Player("Joshoa Kimmich",23,175);

  Player[] Aplayers = {P1,P2,P3};

  Team B = new Team(A , Aplayers ,"FcBayern");

  System.out.println(B.toString());

}

And I'm a barcelona fan by the way hahaha still want the revenge for that 8-2 have a great day !

Imad Saji
  • 1
  • 1
  • Thanks for this solution! It's always nice for me to look at better solutins of advanched coders :P all the best! – Lukas.lmst Jan 10 '21 at 17:28