我试图同时迭代两个数组,如下所示,
首先,如果countriesIterator有下一个元素,则域Iterator将循环播放。 CountryIterator有两个元素,域迭代器可能包含n个元素。 当我循环domainIterator时,我用一个已经循环的值填充一个arraylist。 当循环到达国家迭代器时,我把arraylist放在一个hashmap中。
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只被调用一次,但它应该被调用两次,因为有两个不同的键。我该如何解决这个问题?
答案 0 :(得分:1)
在countriesIterator
的第一次迭代中,您的domainIterator
已到达终点。您可能应该将Iterator<String> domainIterator = selectedDomains.iterator();
添加到countriesIterator
循环中,以便在countriesIterator
的每次迭代时从头开始再次迭代。