简体   繁体   English

为每个值获取单独的记录

[英]Get separate record for each value

I have a list, List<String> myList=new ArrayList<String>(); 我有一个列表, List<String> myList=new ArrayList<String>(); This list contains the list of countries that I am dealing with. 此列表包含我正在处理的国家/地区列表。

I am dealing with several records. 我正在处理几笔记录。 I need to calculate in such a way that a separate entry for records as per country is sorted.I am using the following logic 我需要按以下方式计算:按国家/地区对记录的单独条目进行排序。我使用以下逻辑

for(int zTmp = 0; zTmp<myList.size(); zTmp++)
{
    System.out.println("COUNTRY IS"+myList.get(zTmp));
    if((record).contains(myList.get(zTmp)))
    {  
        // my next step
    }
}

How ever I find that each and every record is entering after the if condition. 我怎么发现每条记录都在if条件之后输入。 The records are alphabetically sorted as per countries, and records of every country are together. 记录按国家/地区的字母顺序排序,并且每个国家/地区的记录都在一起。 Please correct me. 请纠正我。

This is my String 这是我的弦

RECORD 1@India$
RECORD 2@India$
RECORD 3@United Arab Emirates$
RECORD 4@United Arab Emirates$
RECORD 5@United Kingdom$

Sorted as per country name. 按照国家名称排序。 I need to give a condition such that it enters in the loop for every country ie say RECORD 1,RECORD 2 calculation must be done break; 我需要给出一个条件,使它进入每个国家的循环,即说记录1,记录2的计算必须中断; record 3 ,4 break; 记录3,4休息; record 5 like this. 像这样记录5。 Hope I am more clear now. 希望我现在更加清楚。

Maybe you mean this? 也许你是这个意思?

String currentCountry = "";
for (String record : myList) {
    // Regex for entire string "^....$"
    // Country between '@' and '$' (the latter escaped)
    String country = record.replaceFirst("^.*@(.*)\\$$", "$1");
    if (!country.equals(currentCountry)) {
        currentCountry = country;
        ... // Deal with next country
    }
}
for(int zTmp = 0; zTmp<myList.size(); zTmp++)
{
    System.out.println("COUNTRY IS"+myList.get(zTmp));
    if((record).contains(myList.get(zTmp)))
    {  
        // my next step
    }
}

Your if condition will result in false only if record doesn't contain a country which is not present in complete myList , otherwise It will be true atleast in one iteration. 仅当record不包含完整的myList中没有的国家/地区时,您的if条件才会导致false ,否则至少在一次迭代中为true

In comments you wrote: 在评论中,您写道:

You want to calculate 3 records 您要计算3条记录

instead of using myList , create a separate list (say myChoosenCountriesList ) of having those countries only when you want if condition to become true. 而是采用myList ,创建一个单独的列表(说myChoosenCountriesList只有当你想为这些国家的) if条件成为现实。

Then replace your code with following: (Note other improvments also) 然后用以下代码替换您的代码:(还请注意其他改进)

int countryCount = myChoosenCountriesList.size();
for(int zTmp = 0; zTmp<countryCount; zTmp++)
{
    String countryName = myChoosenCountriesList.get(zTmp);
    System.out.println("COUNTRY IS"+countryName);
    if(record.contains(countryName))
    {  
        // my next step
    }
}

The desired output has been achieved by using do while loop here is the snippet 通过使用do while循环已达到所需的输出,这里是代码段

                 int zTmp=0;
                 do  
            {
                String country=myList.get(zTmp);
                if(inputCountry.equals(country))
                {

                    CalcDays(tmpTokens[iTmp]);
                    myDateList.clear();
                }zTmp++;
            }while(zTmp<myList.size());

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

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