如何通过另一个构造函数添加到数组列表?

时间:2019-04-16 13:20:38

标签: java arraylist constructor add

因此,我需要尝试通过驱动程序类中的此构造函数将其添加到我的资格arrayList中。我正在使用扫描仪通过键盘输入所有输入 我是一个初学者程序员,我认为这只是一些基本知识。

public class Driver {

private Scanner sc = new Scanner(System.in);
private Doctor doctor;
private Qualification qualifications;
System.out.println("Enter doctors name: ");
                    sc.nextLine();
                    String name = sc.nextLine();
                    System.out.println("Enter doctors date of birth");
                    String dob = sc.nextLine();
                    System.out.println("Enter doctors gender");
                    String gender = sc.nextLine();
                    System.out.println("Enter doctors address");
                    String address = sc.nextLine();
                    System.out.println("Enter doctors contact number");
                    String contactNumber = sc.nextLine();
                    System.out.println("Was the doctor qualified in Ireland?");
                    boolean qualifiedInIreland = sc.nextBoolean();
                    System.out.println("Add doctor qualifications");



Doctor doctor1 = new General(name, dob, gender, address,contactNumber, qualifiedInIreland, <Qualification> qualifications);
                    med1.addDoctor(doctor1);

2 个答案:

答案 0 :(得分:1)

将它们传递给函数时,无需指定任何类型参数:

Doctor doctor1 = new General(name, dob, gender, address,contactNumber, qualifiedInIreland, qualifications);

答案 1 :(得分:1)

med1对象是哪种类型? 请提供完整代码

您需要将资格存储在Arraylist中,然后将资格的Arraylist传递给Doctor构造函数。

尝试添加此内容

ArrayList<String> qualifications = new ArrayList<String>();
System.out.println("Add doctor qualifications");
while(sc.hasNext){
        qualifications.add(sc.nextLine());  
}

然后

Doctor doctor1 = new General(name, dob, gender, address,contactNumber, qualifiedInIreland, qualifications);