创建返回数组列表的方法



我不知道如何创建一个返回(更新的)数组列表的方法。我想要一个请求用户输入的方法,从输入中创建一个对象,然后把它放在一个列表中。我想让这个方法把这个列表返回给main。

这是我在main方法中所做的:

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<Doctor> listOfDoctors = new ArrayList<>();
ArrayList<Examination> listOfExaminations = new ArrayList<>();
ArrayList<Patient> listOfPatients = new ArrayList<>();      
while (true) {
menu(); //shows options 1 to 5
String option = scanner.nextLine();
if (option.isEmpty()) {
break;
}
if (option.equals("1")) {
System.out.println("What is the name of the doctor?");
String nameDoctor = scanner.nextLine();
System.out.println("What is the doctor's specialisation?");
String specialisationDoctor = scanner.nextLine();
listOfDoctors.add(new Doctor(nameDoctor, specialisationDoctor));
System.out.println("added!");
}

我想使选项1的方法,但我不知道如何。这是我尝试过的:

public static ArrayList<> addDoctorToList(ArrayList<>) {
ArrayList<Doctor> listOfDoctors = new Arraylist<>();
Scanner scanner = new Scanner(System.in);

System.out.println("What is the name of the doctor?");
String nameDoctor = scanner.nextLine();
System.out.println("What is the doctor's specialisation?");
String specialisationDoctor = scanner.nextLine();
listOfDoctors.add(new Doctor(nameDoctor, specialisationDoctor));
System.out.println("added!");
return listOfDoctors;

我希望有人能帮助我。

你的方法不是医生列表类型

因为你返回的是一个doctor对象类型作为你的列表类型,所以你的方法应该是相同的。

修改代码:

public static ArrayList<> addDoctorToList(ArrayList<String> var) {
ArrayList<Doctor> listOfDoctors = new Arraylist

:

public static ArrayList<Doctor> addDoctorToList(ArrayList<String> var) {
ArrayList<Doctor> listOfDoctors = new Arraylist

我想我可以用下面这样一个简单的方法:

public static void insertDoctor(){
System.out.println("What's the name of the doctor?");
String nameDoctor=scanner.nextLine();
System.out.println("What's the doctor's specislisaton?");
String specialisationDoctor=scanner.nextLine();
listOfDoctor.add(new Doctor(nameDoctor, specialisationDoctor));
System.out.println("added!");
}

你觉得怎么样?

你可以做两件事:

  1. 这个方法接受一个已经存在的列表,并给它添加一个Doctor(你可以返回它或者只是修改已经存在的ArrayList)。

在这种情况下,你需要一个名字作为你的参数,而不是创建一个新的ArrayList:

public static ArrayList<Doctor> addDoctorToList(ArrayList<Doctor> listOfDoctors ) {
Scanner scanner = new Scanner(System.in);
...
  1. 方法创建一个新的空数组列表,不需要传递参数,所以删除括号中的类型:
public static ArrayList<Doctor> addDoctorToList() {
ArrayList<Doctor> listOfDoctors = new Arraylist<>();
Scanner scanner = new Scanner(System.in);
...

你差一点。您的代码应该有且只有一个System.inScanner

您正在创建医生的ArrayList,这意味着不止一个医生。您需要将Scanner代码放入循环中,以便用户可以输入多个医生。用户必须能够结束循环,所以我对用户进行了测试,只按Enter键,不输入任何东西。你可以把它改成"quit"或";done"如果你愿意的话。

这是修改后的方法。

public static ArrayList<> addDoctorToList(Scanner scanner) {
ArrayList<Doctor> listOfDoctors = new Arraylist<>();

String nameDoctor = "";
do {
System.out.println("What is the name of the doctor?");
nameDoctor = scanner.nextLine().trim();
if (!nameDoctor.isEmpty()) {
System.out.println("What is the doctor's specialisation?");
String specialisationDoctor = scanner.nextLine().trim();
listOfDoctors.add(new Doctor(nameDoctor, specialisationDoctor));
System.out.println("added!");
}
} while (!nameDoctor.isEmpty());
return listOfDoctors;
}