2013-05-11 54 views
0

我有2个数组列表,其中记录存储为对象。
首先arraylist对象具有诸如患者id,totaltestcost,totaltreatmentcost等属性。 第二个arraylist对象具有诸如患者id,totalservicecharges等属性。在2个数组列表中添加属性

现在我的问题是,当我尝试合计一个给定的患者ID,totaltestcost + totaltreatmentcost + totalservicecharges,我不能加总和显示它。

我发现,因为它是在2个数组中,我需要通过id搜索数组并获得成本。

这是我的搜索病人记录 公共patient_class searchrecord(ArrayList的患者中,字符串搜索) {

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


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

      return patient; 
     } 
    return null; 

}

有,我可以修改这个代码的可能性的方式获得存储在2个数组中的成本?

例如像这样: -

公众诠释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,我需要获得servicecharge费用,但它的另一个阵列。

那么我该如何做到这一点?

谢谢你的时间。

编辑: -

最后我设法做到这家伙,这里是我是如何做到的: -

公共字符串addtotal(ArrayList的患者中,字符串搜索,ArrayListpatient2,字符串搜索2) { INT总;

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; 

}

i的两个阵列通过在一次。

谢谢大家分享你的答案,下一次生病肯定使用树状

+1

在我看来,你正在使用错误的数据结构的工作。 一个地图结构,其中病人ID是关键,价值是病人对象将适合工作更好。它将允许查找O(1)的成本。 – 3xil3 2013-05-11 12:51:38

+0

现在改变结构已经太晚了,我需要在星期一给这个项目。如果可能的话请帮忙。 谢谢 – 2013-05-11 12:54:56

+1

嗯,我想帮忙,但我需要一些更多的信息在第二个数组。我只看到1个阵列描述病人列表。但我不知道第二个看起来像什么。这第二份清单是否也是患者名单?如果是,我们在寻找什么方法? – 3xil3 2013-05-11 12:57:00

回答

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; 
} 

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

1

我想你忘记else关键字。您共有两种方法的将只返回null

你需要somethink这样写:

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; 
} 
+0

谢谢我编辑它,但我不能加总。 – 2013-05-11 13:02:34

1

我会建议更改数据结构的地图。如果您必须使用两个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是一个整数,如果它是一个String,则可以将其更改为.equals。我希望它有帮助。 如果您的类型是patient_class,那么当然会将所有患者更改为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); 

顺便说一句,你可以通过任何数量的清单作为进入参数的方法,只要你指定THEI

public int(ArrayList a, ArrayList b) 

不会编译。