【发布时间】: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