如何在Team类构造函数中正确初始化Player类数组?
Hey Lukas, let's sort out that array initialization issue in your Team constructor right away! The problem with your current code is that your loop is doing thePlayer[i] = thePlayer[i]—you're just assigning the parameter's elements to themselves, which does nothing to initialize the Team class's own thePlayer array.
Here are a few solid ways to correctly initialize the Player array, depending on your needs:
1. Direct Reference (Shallow Copy)
If you don't need the Team's player array to be independent from the one you pass in (meaning changes to the original array will affect the team's array), you can simply assign it directly:
public class Team { Manager theManager; Player[] thePlayer; String name; public Team(Manager theManager, Player[] thePlayer, String name) { this.theManager = theManager; this.thePlayer = thePlayer; // Directly reference the input array this.name = name; } }
Note: This is the simplest approach, but be cautious—if someone modifies the original playerArrFcB or playerArrBvb after creating the Team, those changes will show up in the Team's thePlayer array too.
2. Create a New Array (Shallow Copy)
If you want the Team to have its own separate array (so changes to the original don't affect the team), you can create a new array and copy the elements over. You can do this manually, or use built-in methods for cleaner code:
Manual Copy
public Team(Manager theManager, Player[] thePlayer, String name) { this.theManager = theManager; // Initialize the team's array with the same length as the input this.thePlayer = new Player[thePlayer.length]; // Copy each player reference to the new array for (int i = 0; i < thePlayer.length; i++) { this.thePlayer[i] = thePlayer[i]; } this.name = name; }
Using Arrays.copyOf (Cleaner Built-In Method)
import java.util.Arrays; public class Team { Manager theManager; Player[] thePlayer; String name; public Team(Manager theManager, Player[] thePlayer, String name) { this.theManager = theManager; this.thePlayer = Arrays.copyOf(thePlayer, thePlayer.length); this.name = name; } }
Both of these create a new array, but they still copy references to the original Player objects. If the Player class has mutable fields (like goalsScored that can be updated), changes to a Player instance will still affect both the original array and the team's array.
3. Deep Copy (Full Independence)
If you want complete separation—where changes to either the original array or the team's array, or to individual Player objects, don't affect each other—you need to perform a deep copy. This requires creating new Player instances for the team's array:
Assuming your Player class has a copy constructor or a clone() method, you can do 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++) { // Use a copy constructor to create a new Player instance this.thePlayer[i] = new Player(thePlayer[i]); // Or if you implemented Cloneable: // this.thePlayer[i] = (Player) thePlayer[i].clone(); } this.name = name; }
This way, the Team has its own set of Player objects that aren't linked to the original ones.
Pick the approach that best fits how you plan to use your Team and Player classes!
内容的提问来源于stack exchange,提问作者Lukas.lmst

