Is it not good to increase the length of the loop body when using a java for loop?
某草草
某草草 2017-06-28 09:23:52
0
7
803
   for(CityDataVO cityItem: citys){
        boolean flag =false;
        for(ProvinceDataVO proItem : list){
            xxxxxx
            flag = true;
            break;
        }
        if(!flag){
            ProvinceDataVO province = new ProvinceDataVO();
            province.setProvinceId(cityItem.getProvinceId());
            province.setProvinceName(cityItem.getProvinceName());
            province.setReportNum(cityItem.getCount());
            List<CityDataVO> vo = new ArrayList<>();
            vo.add(cityItem);
            province.setCityData(vo);
            list.add(province);
        }
    }
    

For example, in the above code, if I do not enter the second level of for loop, I will perform an add operation on the list. There should be no problem here, because what I am operating in the first for loop body is the size() of the second for loop. I remember a situation where for(){...} I directly operated the size() of for in... This seems to be impossible. There seemed to be some way at the beginning, but I can't think of it for the time being. Can any master Help me recall the memories? ?

某草草
某草草

reply all(7)
过去多啦不再A梦

Don’t use for(:){}, use for(int i = 0; i < list.size(); i++), be careful not to create an infinite loop

大家讲道理

In the for each, that is, the for(a : as) loop in your code, you cannot add or delete the looped collection, otherwise a ConcurrentModificationException will be reported.
There is no problem in this code. Because all your operations of changing the list are done outside the inner loop.

Depending on your needs is actually to count the data of each city by province. If you have the conditions to use Java 8, you can take a look at the GroupBy method of Stream. Can greatly simplify the code.

For some unrelated issues, the set method to set a list is not appropriate.

  • If it is an object with business logic, the internal structure should be packaged and provide an interface based on the concepts of the business domain, and the internal collection should not be directly exposed.

  • Even if it is a data transfer object, it should not provide the set method of the collection attribute. In general, the life cycle of a private collection variable should be managed by its parent object. The outside world operates the private collection through the add or remove method of the outer packaging object. If necessary, provide a collection get method. You need to consider whether to copy or make it immutable.

曾经蜡笔没有小新

You cannot operate list (add, remove, etc.) in foreach

伊谢尔伦

If you want to jump out of the two-level loop directly, there are only two ways:

  1. Use the syntax of break label (I have never used this before, you can Google it);

  2. That’s the method you use, set a flag.

Also, the plural of city is cities not citys.

世界只因有你

This is a long way to write. My words will be written like this:

for (CityDataVO cityItem: citys){

    if (validateCityItem(cityItem, list)) {
        continue;
    }

    List<CityDataVO> vo = new ArrayList<>();
    vo.add(cityItem);

    ProvinceDataVO province = new ProvinceDataVO();
    province.setProvinceId(cityItem.getProvinceId());
    province.setProvinceName(cityItem.getProvinceName());
    province.setReportNum(cityItem.getCount());    
    province.setCityData(vo);
    
    list.add(province);
}

...

private boolean validateCityItem(CityDataVO cityItem, List<ProvinceDataVO> list) {
    for(ProvinceDataVO proItem : list){
        if (...) {
            return true;
        }
    }
    
    return false;
}

Your original code has several areas that need optimization:

  1. Try to avoid double loops, the inner loop should generally be extracted;

  2. For operations on the same variable, the lines of code should be grouped together as much as possible, so that it reads more naturally;

  3. Variable naming should reflect its business meaning and main type. For example, List objects should be named xxxList as much as possible. I'm too lazy to change this for you.

为情所困

I don’t understand what you are asking..Is it because if can’t get in?

滿天的星座

Organize your thoughts before asking questions. What do you want to ask? What answer do you hope to get? Otherwise, others won’t be able to answer it for you.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!