在2个arraylists中添加属性

时间:2013-05-11 12:46:41

标签: java arrays loops arraylist

我有2个arraylist,记录存储为对象 第一个arraylist对象具有患者id,totaltestcost,totaltreatmentcost等属性。 第二个arraylist对象具有患者id,总服务费等属性。

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

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

这是我搜索病历的方式  public patient_class searchrecord(ArrayList patients,String search)  {

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


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

            return patient;
        }
    return null;

}

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

例如: -

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

}

我立刻传入了两个阵列。

感谢大家分享您的答案,下次生病肯定会使用树图

3 个答案:

答案 0 :(得分:1)

我认为您忘记了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;
}

答案 1 :(得分: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是一个整数,你可以将它改为.equals,如果它是一个String。我希望它有所帮助。 如果您的类型是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);

顺便说一下,只要指定了

,就可以将任意数量的列表作为参数传递给方法
public int(ArrayList a, ArrayList b)

不会编译。

答案 2 :(得分: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;
 }

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

相关问题