【问题标题】:How to create constructor with empty ArrayList?如何使用空的 ArrayList 创建构造函数?
【发布时间】:2014-04-11 21:09:53
【问题描述】:

我写了一个可以传递所有字段的构造函数,包括一个Arraylist。如果我想要不传递 Arraylist 而是给它一个空的 Arraylist 的构造函数,我不知道该怎么做。

例如,我已经写了一个 Course 类,现在我正在写一个 Student 类。学生类包含一个 Arraylist。

class Student {
    String studentFirstName;
    String studentLastName;
    ArrayList<Course> studentSchedule = new ArrayList<Course>();

    // Constructors
    Student(String newFirstName,  String newLastName) {
        this.Student(newFirstName, newLastName, _______ ); //what to put in the blank?
    }

    Student(String newFirstName, String newLastName, ArrayList<Course> newSchedule) {
        this.studentFirstName = newFirstName;
        this.studentLastName = newLastName;
        this.studentSchedule = newSchedule;
    }
    .
    .
    .

我被困在这里了。将 null 放在空白处不起作用,我收到编译器警告:Student 类型的方法 Student(String, String, null) 未定义显然我没有抓住重点。

如何让构造函数给我一个空的 Arraylist?

【问题讨论】:

    标签: java arraylist constructor


    【解决方案1】:

    好吧,你想传递一个空的ArrayList,所以传递一个空的ArrayList:

    class Student {
        String studentFirstName;
        String studentLastName;
        List<Course> studentSchedule; // no initialization here: it's done in the constructor
    
        // Constructors
        Student(String newFirstName,  String newLastName) {
            this(newFirstName, newLastName, new ArrayList<Course>());
        }
    
        Student(String newFirstName, String newLastName, List<Course> newSchedule) {
            this.studentFirstName = newFirstName;
            this.studentLastName = newLastName;
            this.studentSchedule = newSchedule;
        }
    

    请注意,您通常应该将该字段声明为List&lt;Course&gt;,而不是ArrayList&lt;Course&gt;,除非列表非常重要的是一个ArrayList,而不是任何其他类型的列表。接口上的程序。

    此外,您的字段应该是私有的,并且不应该被命名为 studentXxx。它们是 Student 类的一部分,因此是多余的。 lastName 就足够了,而且更红。

    【讨论】:

    • +1 用于提及List 接口和最佳命名实践。还可以考虑使用像this.studentSchedule = new ArrayList&lt;Course&gt;(newSchedule); 这样的防御性副本,以防止列表在传递给构造函数后被修改。
    【解决方案2】:

    传递一个空的ArrayList

    this(newFirstName, newLastName, new ArrayList<Course>());
    

    (还要注意正确的语法是this.,而不是this.Student.

    【讨论】:

      【解决方案3】:

      只需构造一个空的ArrayList 并将其传递给构造函数:

      Student(String newFirstName,  String newLastName) {
          this(newFirstName, newLastName, new ArrayList<>());
      }
      

      另外,请注意我使用this() 调用Student 的其他构造函数,而不是无效的this.Student()

      但请注意,您已经在声明时初始化了 ArrayList 字段。如果您希望它通过构造函数进行,则应考虑删除此初始化。

      【讨论】:

        【解决方案4】:

        如果我猜对了,可能会传递Arraylist 的新空实例是你的问题

        this (newFirstName, newLastName, new ArrayList<Course>())
        

        或者,你也可以通过null

        this (newFirstName, newLastName, null)
        

        编辑:

        在@Erwin 的注释之后,要调用同一个类的构造函数,你应该只使用this()

        【讨论】:

        • 'this.Student(' 不编译。
        猜你喜欢
        • 2018-10-15
        • 2017-03-04
        • 1970-01-01
        • 1970-01-01
        • 2021-07-17
        • 1970-01-01
        • 1970-01-01
        • 2013-08-24
        • 1970-01-01
        相关资源
        最近更新 更多