Java 如何使用空的 ArrayList 创建构造函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22271801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 14:39:11  来源:igfitidea点击:

How to create constructor with empty ArrayList?

javaarraylistconstructor

提问by rawr rang

I wrote a constructors that can passes all the fields, including an Arraylist. I don't know what to do if I want constructors where I'm not passing the Arraylist and instead give it an empty Arraylist.

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

For example I already wrote a Course class and now I'm writing a Student class. The student class contains an Arraylist.

例如,我已经编写了一个 Course 类,现在我正在编写一个 Student 类。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;
    }
    .
    .
    .

I'm stuck here. Putting null in the blank does not work, I get compiler warning: The method Student(String, String, null) is undefined for the type Student Obviously I'm missing the point.

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

How do I get the constructor to give me an empty Arraylist?

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

采纳答案by JB Nizet

Well, you want to pass an empty ArrayList, so pass an empty ArrayList:

好吧,你想传递一个空的 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;
    }

Note that you should generally declare the field as List<Course>, not as ArrayList<Course>, unless it's really important for the list to be an ArrayList, and not any other kind of list. Program on interfaces.

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

Also, your fields should be private, and shouldn't be named studentXxx. They're part of the Student class, so it's redundant. lastNameis sufficient, and more redable.

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

回答by NPE

To pass an empty ArrayList:

传递一个空的ArrayList

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

(Also note that the correct syntax is this., not this.Student.)

(另请注意,正确的语法是this.,不是this.Student.

回答by Not a bug

Probably passing new empty instance of Arraylistis your question if I got it correct

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

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

Or, you can pass nullalso,

或者,你也可以通过null

this (newFirstName, newLastName, null)

Edit :

编辑 :

After @Erwin's comment, to invoke constructor of same class, you should use this()only.

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

回答by arshajii

Just construct an empty ArrayListand pass it to the constructor:

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

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

Also, notice that I've used this()to call Student's other constructor, as opposed to this.Student()which is invalid.

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

Notice, though, that you are already initializing the ArrayListfield at the point of declaration. You should consider removing this initialization if you want it to take place via the constructor.

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