【问题标题】:Adding attributes in 2 arraylists在 2 个数组列表中添加属性
【发布时间】:2013-05-11 12:46:41
【问题描述】:

我有 2 个数组列表,其中记录存储为对象。
第一个 arraylist 对象具有患者 id、totaltestcost、totaltreatmentcost 等属性。 第二个 arraylist 对象具有患者 id、totalservicecharges 等属性。

现在我的问题是当我尝试将总数相加时,即 totaltestcost + totaltreatmentcost + totalservicecharges,对于给定的患者 ID,我无法将总数相加并显示它。

我发现由于它在 2 个数组中,我需要按 id 搜索数组并获取成本。

这就是我搜索病历的方式 公共患者类搜索记录(ArrayList 患者,字符串搜索) {

    for(patient_class patient: patients) //used  enhanced for loop


        if(patient.getid().equals(search))
        {

            return patient;
        }
    return null;

}

是否有可能我可以修改此代码以将成本存储在 2 个数组中?

例如像这样:-

public int searchrecord(ArrayList 患者,字符串搜索) {

    for(patient_class patient: patients) //used  enhanced for loop


        if(patient.getid().equals(search))
        {

           int total= patient.gettotaltestcost + patient.gettotaltreatmentcost
        }
    return null;

} 但是如果我使用上述方法只能得到 totaltestcost+totaltreatmentcost 的总和,我还需要获取 servicechargecost 但它在另一个数组中。

那我该怎么做呢?

感谢您的宝贵时间。

编辑:-

我终于做到了,伙计们,我是这样做的:-

public String addtotal(ArrayList patients, String search,ArrayListpatient2,String search2) { 总计;

    for(patient_class patient: patients) //used  enhanced for loop

    {
        if(patient.getid().equals(search))
        {



        for(patient_class patient3: patient2)
        {
            if(patient3.getid().equals(search2))
            {
                total=patient.gettreatmentcost()+patient.gettestcost()+patient3.getservicecharge();

                return Double.toString(total);
            }
        }
        }
    }

    return null;

}

我同时传入了两个数组。

感谢大家分享你的答案,下次生病一定要使用树形图

【问题讨论】:

  • 在我看来,您为这项工作使用了错误的数据结构。患者 id 是键,值是患者对象的映射结构将更适合这项工作。这将允许查找成本为 O(1)。
  • 现在改变结构为时已晚,我需要在即将到来的星期一提交这个项目。如果可能,请提供帮助。谢谢
  • 好吧,我想帮忙,但我需要有关第二个数组的更多信息。我只看到一个描述患者列表的数组。但我不知道第二个是什么样子。第二个列表是否也是患者列表,如果是,我们在寻找什么方法?

标签: java arrays loops arraylist


【解决方案1】:

您的代码有很多问题,但是对您的功能进行编码并无视正确的java coding conventions(您显然出于某种原因忽略了它,我不会修复它)。我想出了这个。 1 种方法可在您的列表中找到您的患者,1 种方法可从 2 个列表中计算患者总数。

public patient_class findPatient(List<patient_class> patients, String search) {
    for(patient_class patient: patients) //used  enhanced for loop
        if(patient.getid().equals(search))
        {
           return patient;
        }
    }
    return null; // no patient found matching the search id
 }

 public int calculatePatientTotal(List<patient_class> patientsList1, List<patient_class> patientsList2, String search){
    patient_class patientInList1 = findPatient(patientsList1,search);// assuming unique id
    patient_class patientInList2 = findPatient(patientsList2,search);// assuming unique id

    int total=0;
    // calc general total
    if(patientInList1!=null{
        // I'm assuming you put these getters in methods instead of public properties!
        // hence the brackets at the end
        total= patientInList1.gettotaltestcost() + patientInList1.gettotaltreatmentcost();
    } 

    // add any extra charges
    if(patientInList2!=null ){
        // I'm assuming this is the method that calculates the other charges in that patient object.
        total+= patientInList2.getOtherCharges();
    }

    return total;
 }

下次我建议您考虑使用适当的结构,主要是不要将相同的信息分散到不同的项目上。

【讨论】:

    【解决方案2】:

    我认为您忘记了 else 关键字。你所有的两种方法都将只返回null

    你需要这样写:

    public patient_class searchrecord(ArrayList patients, String search) {
    
    for(patient_class patient: patients) //used  enhanced for loop
    
    
        if(patient.getid().equals(search))
        {
            return patient;
        }
        else 
            return null;
    }
    

    还有这个

    public int searchrecord(ArrayList patients, String search) {
    
    for(patient_class patient: patients) //used  enhanced for loop
    
    
        if(patient.getid().equals(search))
        {
           int total= patient.gettotaltestcost + patient.gettotaltreatmentcost;
           return total;
        }
        else
           return null;
    }
    

    【讨论】:

      【解决方案3】:

      我建议更改地图的数据结构。如果您必须使用两个 arrayLists,您可以编写一个方法,将所需的结果作为整数返回。 效率太低了。

      public static int total(List<Patient> firstList, List<Patient> secondList, int search)
      {
          int total = 0;
      
          for(Patient p:firstList)
          {
              if(p.getId() == search)
              {
                  total = p.getServiceCost();
                  for(Patient p2 : secondList)
                  {
                      if(p2.getId()== search)
                      {
                          total += p2.gettotaltestcost() + p2.gettotaltreatmentcost();
                          break;
                      }
                  }
              }
          }
          return total;
      }
      

      这是我的想法,没有经过测试,所以你可能有错误。我假设 id 是一个整数,如果它是一个字符串,您可以将其更改为 .equals。我希望它有所帮助。 如果您的类型是 Patient_class,那么当然将所有 Patient 更改为 Patient_class。在您的主要内容中,您可以执行类似

      的操作
      List<patient_class> list1 = new ArrayList<patient_class>();
      List<patient_class> list2 = new ArrayList<patient_class>();
      // populate those lists 
      int search = 12;    
      int result = total(list1, list2, int search);
      

      顺便说一句,只要您指定 i,您就可以将任意数量的列表作为参数传递给方法

      public int(ArrayList a, ArrayList b)
      

      无法编译。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-01-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多