[英]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.