2016-04-15 71 views
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只被调用一次,但它应该被调用两次,因为有两个不同的键。我怎样才能解决这个问题?

回答

1

由第一次迭代countriesIterator你有你的domainIterator到达尾声。您可能应该将Iterator<String> domainIterator = selectedDomains.iterator();包含到countriesIterator循环中,以便在每次迭代countriesIterator时开始重新开始迭代。

+0

更新,与迭代器的名字搞砸了。 –

+0

是的,它的工作原理......谢谢......但有一个问题 –

+0

现在,这个数组看起来像新加坡= [13,4],印度= [13,4],但我希望它像新加坡[4],印度[13] ... –