简体   繁体   English

从测试文件向类中的数组添加对象?

[英]Adding objects to an Array in a class from a test file?

Unsure on how i'm supposed to add to an array, i've been asked to fill the array from a test file but define it in class Patient. 不确定我应该如何添加到数组中,有人要求我从测试文件中填充数组,但要在Patient类中对其进行定义。 Any ideas? 有任何想法吗?

   public class Patient
   {
   private String name;
   private int id;
   private int current = 1;
   public Patient(String name, int id)
   {
      this.name = name;
      this.id = id;
      Patient[] patient = new Patient[100];
      String[] Observations;
      System.out.print(patient[0]);
   }
  public String addPatient(String name,int id)
  {
     Patient[current-1] = new Patient(name,id);
  }
} 

// extract from class  PatientRecordSystem
public void addPatient()
{
  String name = "James";
  int id = 10122;
  Patient patient = new Patient(name, id);
}

Your problem is that you are defining that list (or array) to hold Patient objects within the constructor of your Patient class. 您的问题是您正在定义该列表(或数组)以将Patient对象保存在Patient类的构造函数中。

That is simply wrong on many levels. 这在很多层面上都是错误的。 First of all - one "Patient" should be exactly that - the representation of a single patient . 首先-一个“病人”应该就是那个-单个病人的代表。 When you go to the doctor and become a patient ... are you asked to know about 100 other patients around?! 当您去看医生并成为患者时...您是否被要求了解周围的其他100位患者? Then: that array that you define in the constructor ... just lives during the execution of the constructor. 然后:您在构造函数中定义的数组...仅在构造函数执行期间存在。 It simply goes away as soon as a call 来电一响,它就消失了

Patient newPatient = new Patient( ... ) 

returns. 返回。

In other words: you want to think of another class that is responsible for "managing" multiple patients. 换句话说:您想考虑另一个负责“管理”多个患者的类。 And then you create "patient objects"; 然后创建“患者对象”; and tell the manager about them. 并告诉经理他们。 And that "manager" is then using an array (or better some more dynamic List) in order to keep track of "managed" patients. 然后,该“经理”将使用一个数组(或者更好的是一些动态列表)来跟踪“受管理”的患者。

Whatever Andrew said is correct , just making it easy for you. 安德鲁所说的都是对的,只是对您而言很容易。 Use below code 使用以下代码

  public class Patient
   {
   private String name;
   private int id;
   private int current = 1;
   private Patient[] patient = new Patient[100];
   public Patient(String name, int id)
   {
      this.name = name;
      this.id = id;

      String[] Observations;
      System.out.print(patient[0]);
   }
  public String addPatient(String name,int id)
  {
     patient[current-1] = new Patient(name,id);
  }
} 

// extract from class  PatientRecordSystem
public void addPatient()
{
  String name = "James";
  int id = 10122;
  Patient patient = new Patient(name, id);
}

First you need to declare the array. 首先,您需要声明数组。

You can do something like: 您可以执行以下操作:

Patient[] patients = new Patient[100];

if you know the size of the array. 如果您知道数组的大小。

If you want to build a dynamic array, because you don't know how many elements you are going to have, you can do something like that. 如果您想构建一个动态数组,因为您不知道将要拥有多少个元素,则可以执行类似的操作。

List<Patient> patients = new ArrayList<Patient>();

Then you can assign values to the array: 然后,您可以将值分配给数组:

If you have declared a fixed array you can do something like that: 如果声明了固定数组,则可以执行以下操作:

patients[0] = new Patient(name, id); 病人[0] =新病人(姓名,身份证);

On the other hand, if you have declared a dynamic array, the code would look like: 另一方面,如果您声明了动态数组,则代码将类似于:

patients.add(new Patient(name, id));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM