如何为另一个类的构造函数中的arraylist添加值 Employee.java
public Employee(String name, String job, int age, String email, ArrayList<String> phoneNumbers)
{}
Dataentry.java
public List<Employee> createListEmployees(){
List<Employee> retVal = new ArrayList();
Employee( String name, String job, int age, String email, ArrayList<String>
phoneNumbers);
retVal.add(new Employee("Bob", "Technician", 33, "bob@bobemail.com",
("555123123","5553213213")));
retVal.add(new Employee("Jane", "Accountant", 33, "jane@mail.com",
["525123123","5553413213"]));
答案 0 :(得分:1)
您需要为电话号码正确构建ArrayList
:
List<Employee> retVal = new ArrayList<>();
retVal.add(new Employee("Bob", "Technician", 33, "bob@bobemail.com",
new ArrayList<>(Arrays.asList("555123123","5553213213"))));
如果要更改构造函数签名以使用List
接口类型,事情会变得更简单:
public Employee(String name, String job, int age, String email,
List<String> phoneNumbers) {
/* ... */
}
List<Employee> retVal = new ArrayList<>();
retVal.add(new Employee("Bob", "Technician", 33, "bob@bobemail.com",
Arrays.asList("555123123","5553213213")));