繁体   English   中英

在2个数组列表中添加属性

[英]Adding attributes in 2 arraylists

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

现在我的问题是,当我尝试合计总计时,即对于给定的患者编号,合计总测试成本+总治疗成本+总服务费用,我无法将合计并显示出来。

我发现,由于它位于2个数组中,因此需要按ID搜索数组并获取成本。

这是我搜索患者记录的方式public public_class searchrecord(ArrayList Patient,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患者,字符串搜索){

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


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

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

}但是,如果我使用上述方法仅能获得totaltestcost + totaltreatmentcost的总和,那么我还需要获取服务费成本,但它需要包含在另一个数组中。

那我该怎么做呢?

感谢您的时间。

编辑:-

最终我设法做到了,这就是我的做法:-

public String addtotal(ArrayList Patient,String search,ArrayList Patient2,String search2){int total;

    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;

}

我一次通过了两个数组。

感谢大家分享您的答案,下次一定会使用树状图

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

我建议更改地图的数据结构。 如果必须使用两个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都更改为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)

不会编译。

您的代码有很多问题,但是对您的功能进行了编码,却无视适当的Java编码约定 (由于某些原因,您显然会忽略它们,而我将不予解决)。 我想到了这个。 在列表中找到患者的一种方法,以及从这两个列表中计算患者总数的一种方法。

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

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

暂无
暂无

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

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