0
我試着同時遍歷兩個數組如下,與while循環循環,同時從數據庫中獲取記錄不正常
首先,如果countriesIterator得到了下一個元素,域迭代器會被循環。 CountryIterator有兩個元素,域迭代器可能包含n個元素。 當Im循環domainIterator時,我用我已經循環的值填充一個數組列表。 並且當循環到達國家迭代器時,我把數組列表放在一個散列表中。
Iterator<String> domainIterator = selectedDomains.iterator();
Iterator<String> countriesIterator = selectedCountries.iterator();
filteredComplianceCount = new ArrayList<Integer>();
inProgressComplianceCount = new ArrayList<Integer>();
delayedComplianceCount = new ArrayList<Integer>();
nonComplianceCount = new ArrayList<Integer>();
//Looping Countries
while (countriesIterator.hasNext()) {
String countryKey = countriesIterator.next();
Country country = aparjithaDb.getCountryId(countryKey);
//Looping Domains
while (domainIterator.hasNext()) {
Domain domain = aparjithaDb.getDomain(domainIterator.next());
int domainId = domain.getDomainId();
int countryId = country.getCountryId();
//fetch datas from db based on country and domain id/
List<ChartData> allChartCDCounts = db.getAllChartCDCounts(countryId, domainId);
//iterate the list to get the count values
for (ChartData al : allChartCDCounts) {
int complied_count = al.getComplied_count();
int delayed_compliance_count = al.getDelayed_compliance_count();
int not_complied_count = al.getNot_complied_count();
int inprogress_compliance_count = al.getInprogress_compliance_count();
//add the count values to an arraylist
filteredComplianceCount.add(complied_count);
delayedComplianceCount.add(delayed_compliance_count);
inProgressComplianceCount.add(inprogress_compliance_count);
nonComplianceCount.add(not_complied_count);
}
}
//put the arraylist with in hashmap
compMap.put(countryKey, filteredComplianceCount);
delayedCompMap.put(countryKey, delayedComplianceCount);
inProgMap.put(countryKey, inProgressComplianceCount);
nonCompMap.put(countryKey, nonComplianceCount);
}
我的代碼的問題是,一個HashMap的關鍵仍然是唯一的(該鍵添加值後兩個不同的國名),但數值仍然是這兩個鍵相同。域Iterator只被調用一次,但它應該被調用兩次,因爲有兩個不同的鍵。我怎樣才能解決這個問題?
更新,與迭代器的名字搞砸了。 –
是的,它的工作原理......謝謝......但有一個問題 –
現在,這個數組看起來像新加坡= [13,4],印度= [13,4],但我希望它像新加坡[4],印度[13] ... –