获取每个值的单独记录

时间:2022-08-08 12:44:56

I have a list, List<String> myList=new ArrayList<String>(); This list contains the list of countries that I am dealing with.

我有一个列表,List myList = new ArrayList ();此列表包含我正在处理的国家/地区列表。

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. The records are alphabetically sorted as per countries, and records of every country are together. Please correct me.

我怎么发现每条记录都是在if条件之后进入的。记录按国家/地区按字母顺序排序,每个国家/地区的记录在一起。请指正。

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; record 3 ,4 break; record 5 like this. Hope I am more clear now.

按国家/地区名称排序。我需要给出一个条件,使它进入每个国家的循环,即说RECORD 1,RECORD 2计算必须打破;记录3,4,休息;像这样记录5。希望我现在更清楚了。

3 个解决方案

#1


0  

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
    }
}

#2


0  

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.

只有当记录不包含完整myList中不存在的国家/地区时,if条件才会导致false,否则它将在一次迭代中至少为真。

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),只有当你想要条件成为真时才有这些国家。

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
    }
}

#3


0  

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());

#1


0  

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
    }
}

#2


0  

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.

只有当记录不包含完整myList中不存在的国家/地区时,if条件才会导致false,否则它将在一次迭代中至少为真。

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),只有当你想要条件成为真时才有这些国家。

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
    }
}

#3


0  

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());