在ArrayList类型的变量中引入元素

时间:2017-08-24 03:58:28

标签: java list class oop arraylist

我必须通过一种方法(在医院类内)列出患者姓名(使用ArrayList)。我试着写代码但是我被卡住了。首先,我想我应该在ArrayList中添加名称,然后我想我应该展示它们。但我该怎么做呢?

class Hospital
{ Patient p;


 class Patient
 {

 private String name,adress;  
 private int age,phoneNumber,securityNumber;
 private char sex;

 Patient(String name,char sex,String adress,int age,int 
 phoneNumber,int securityNumber)
 {
 this.name=name;
 this.adress=adress;
 this.age=age;
 this.sex=sex;
 this.phoneNumber=phoneNumber;
 this.securityNumber=securityNumber;
 }

 String getName()
 {
     return name;
 }
 void setName(String nam)
 {
 name=nam;
 }

 void ShowListOfNames(List <Patient> pat)
 {
 for(Patient h:pat)
  System.out.println(h.getName());
  }
 }


 }



 public class Tarea23_1ago
 {
public static void main(String[] args)
{
Scanner x=new Scanner(System.in);
List  <Patient> pat=new ArrayList <Patient>();
pat.add(new Patient("dan",'f',"nuyoo",12,2356445,001));
pat.add(new Patient("fredy",'m',"idk",34,34657,002));
pat.add(new Patient("dan",'m',"west",21,345546,003));



}

2 个答案:

答案 0 :(得分:2)

简单解决方案 - 使用此

 void ShowListOfNames(List <Hospital> patient)
 {
    for(Hospital h:patient)
      System.out.println(h.getName());
    }
} 

答案 1 :(得分:1)

// First of all, these data are of a patient, not of a hospital, so I renamed the class...
public class Patient
{
    private String name,address;
    private int age, phoneNumber, securityNumber;
    private char sex;

    // No need a no-argument-constructor
    public Patient(String name, char sex, String address, int age, int
            phoneNumber, int securityNumber)
    {
        // Just to remember: Java generates a super() call even if you do not write it out
        super();
        this.name = name;
        this.address = address;
        this.age = age;
        this.sex = sex;
        this.phoneNumber = phoneNumber;
        this.securityNumber = securityNumber;
    }

    public String getName() { return name; }
    public void setName(String name) { this.name=name; }
}

// Now comes the part that is about more patients. Could be named a hospital...
public class Hospital
{
    // A list of patents
    private final List<Patient> patients =  new ArrayList<>();

    // Prints out the names of the patents on the list
    public void showListOfNames()
    {
        for(final Patient : this.patients)
        {
            System.out.println(patient.getName());
        }
    }

    // The main method. It does not have to be in a separated class...
    public static void main(String[] args)
    {
        final Scanner x = new Scanner(System.in); // Still not in use...
        final Hospital h = new Hospital(); // our hospital instance, with the list of patients

        // Adding patents to the hospital's list
        h.patients.add(new Patient("dan",'f',"nuyoo",12,2356445,001));
        h.patient.add(new Patient("fredy",'m',"idk",34,34657,002));
        h.patient.add(new Patient("dan",'m',"west",21,345546,003));

        // Printing out the names
        showListOfNames();
    }

}