search

Home  >  Q&A  >  body text

Is it not good to increase the length of the loop body when using a java for loop?

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

某草草某草草2708 days ago864

reply all(7)I'll reply

  • 过去多啦不再A梦

    过去多啦不再A梦2017-06-28 09:25:52

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

    reply
    0
  • 大家讲道理

    大家讲道理2017-06-28 09:25:52

    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.

    reply
    0
  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-06-28 09:25:52

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

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-06-28 09:25:52

    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.

    reply
    0
  • 世界只因有你

    世界只因有你2017-06-28 09:25:52

    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.

    reply
    0
  • 为情所困

    为情所困2017-06-28 09:25:52

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

    reply
    0
  • 滿天的星座

    滿天的星座2017-06-28 09:25:52

    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.

    reply
    0
  • Cancelreply