【问题标题】:How to add to an array list through another constructor?如何通过另一个构造函数添加到数组列表?
【发布时间】:2019-09-06 14:49:18
【问题描述】:

所以我需要尝试通过我的驱动程序类中的这个构造函数添加到我的限定数组列表中。我正在使用扫描仪通过键盘输入所有输入 我是一个初学者程序员,我认为它只是一些基本的东西。

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);

【问题讨论】:

    标签: java oop arraylist collections constructor


    【解决方案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);
    

    【讨论】:

    • 好的,谢谢。 med1 在开头声明。 private DoctorAPI med1 = new DoctorAPI();
    【解决方案2】:

    在将它们传递给函数时,您不需要指定任何类型参数:

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

    【讨论】:

      猜你喜欢
      • 2021-06-15
      • 2020-09-03
      • 2015-06-12
      • 2013-11-28
      • 2021-12-20
      • 2010-11-25
      • 2013-03-15
      • 1970-01-01
      • 2012-10-26
      相关资源
      最近更新 更多