【问题标题】:Introducing elements in a variable of type ArrayList在 ArrayList 类型的变量中引入元素
【发布时间】:2018-02-01 19:26:38
【问题描述】:

我必须通过一个方法(在 Hospital 类中)列出患者姓名(使用 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));



}

【问题讨论】:

  • 您从未使用过您声明的扫描仪
  • 不,但这不是问题,问题是我必须编写代码来使用 ArrayList 介绍患者姓名。 @ꜱᴜʀᴇꜱʜᴀᴛᴛᴀ
  • 你想让ShowListOfNames做什么? “显示”(打印)名称列表?创建名称列表?将姓名列表返回给调用者?完全不清楚您要完成什么。
  • @ajb 返回或显示(无论)我在主类中已有的名称,
  • Java 不理解“随便”。你必须做出决定。这是你的第一份工作。

标签: java list class oop arraylist


【解决方案1】:

简单的解决方案-使用这个

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

【讨论】:

    【解决方案2】:
    // 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();
        }
    
    }
    

    【讨论】:

    • 是的,我也解决了关于患者和医院的困惑
    • 你为什么使用final?这里:for(final Patient : this.patients)
    • 是的。 final 表示该变量不能在它所在的范围内重新分配。在增强的for语句中,变量作用域在循环块内,没有赋值,所以可以是final...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-12
    • 2021-03-06
    • 1970-01-01
    • 2021-12-30
    • 2020-10-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多