如何在Java的构造函数中创建一个空数组

huangapple go评论79阅读模式
英文:

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 &lt;TeamMember&gt; members;
       public ProjectTeam(String newName,ArrayList&lt;TeamMember&gt;members){
           name = this.newName;
           members =this.members;
    }
        ProjectTeam team1 = new ProjectTeam(&quot;team1&quot;);
ArrayList&lt;ProjectTeam&gt; traems=new ArrayList&lt;ProjectTeam&gt;();
traems.add(team1)

check the code I don't know if it works fine or not

huangapple
  • 本文由 发表于 2020年3月16日 19:14:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/60704926.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定