英文:
How to create an empty array in constructor java
问题
我有一个作业,在其中创建了两个类。TeamMember 和 ProjectTeam。然而,在创建一个类型为 TeamMember 的空数组时遇到了问题。
class TeamMember {
protected String name;
protected ProjectTeam team;
TeamMember(String newName, ProjectTeam newTeam){
name = newName;
team = newTeam;
}
class ProjectTeam {
protected String name;
protected TeamMember[] members;
public ProjectTeam(String newName){
name = newName;
members = 在这里填写;
}
public class Main {
public static void main(String[] args) {
ProjectTeam team1 = new ProjectTeam("team1");
TeamMember peter = new TeamMember("Peter", team1);
System.out.println(peter.name);
System.out.println(team1.members);
}
}
我尝试的是创建一个 TeamMember,并将其传递给 ProjectTeam。然而,我似乎找不到一种在 ProjectTeam 构造函数中传递 TeamMember 类型的空数组的方法。欢迎提供任何反馈。
英文:
I have a homework where I created 2 classes. TeamMember and ProjectTeam. However I have a problem with creating an empty array of type TeamMember.
class TeamMember {
protected String name;
protected ProjectTeam team;
TeamMember(String newName, ProjectTeam newTeam){
name = newName;
team = newTeam;
}
class ProjectTeam {
protected String name;
protected TeamMember[] members;
public ProjectTeam(String newName){
name = newName;
members = help here;
}
public class Main {
public static void main(String[] args) {
ProjectTeam team1 = new ProjectTeam("team1");
TeamMember peter = new TeamMember("Peter", team1);
System.out.println(peter.name);
System.out.println(team1.members);
}
}
What I am trying to do is to create a TeamMember and pass it to a ProjectTeam. However I cant seem to find a way how to pass an empty array of type Team Member in construcor of ProjectTeam. Any feedback would be appreciated.
答案1
得分: 1
根据注释,也许你应该以不同的方式进行建模,特别是如果你需要添加/删除元素,但是这里是如何创建零长度数组的方式。有时候,很少见,你会需要这样做。它和任何其他长度一样:
String[] myArray = new String[0];
或者:
String[] myArray = new String[]{}; // 空数组初始化
英文:
As the comments say, maybe you should model this differently, especially if you need to add/remove elements, but here is how you create a zero-length array. Sometimes, rarely, you need that. It's the same as any other length:
String[] myArray = new String[0];
Alternatively:
String[] myArray = new String[]{}; // empty array initializer
答案2
得分: 0
尝试使用ArrayList来连接TeamMember和ProjectTeam之间的关系
class TeamMember {
protected String name;
protected ProjectTeam team;
TeamMember(String newName, ProjectTeam newTeam){
name = this.newName;
team = this.newTeam;
}
}
class ProjectTeam {
protected String name;
protected ArrayList<TeamMember> members;
public ProjectTeam(String newName, ArrayList<TeamMember> members){
name = this.newName;
members = this.members;
}
}
ProjectTeam team1 = new ProjectTeam("team1", new ArrayList<TeamMember>());
ArrayList<ProjectTeam> teams = new ArrayList<ProjectTeam>();
teams.add(team1);
// 检查代码,我不知道它是否正常工作
英文:
try to used ArrayList to the connection between the TeamMember and ProjectTeam
class TeamMember {
protected String name;
protected ProjectTeam team;
TeamMember(String newName, ProjectTeam newTeam){
name = this.newName;
team = this.newTeam;
}
class ProjectTeam {
protected String name;
protected ArrayList <TeamMember> members;
public ProjectTeam(String newName,ArrayList<TeamMember>members){
name = this.newName;
members =this.members;
}
ProjectTeam team1 = new ProjectTeam("team1");
ArrayList<ProjectTeam> traems=new ArrayList<ProjectTeam>();
traems.add(team1)
check the code I don't know if it works fine or not
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论