简体   繁体   English

在Java中动态更改循环范围

[英]Dynamically change for-loop range in Java

Is there a way to increase the for-loop range in java? 有没有办法增加Java中的for循环范围? I have the following code: 我有以下代码:

for (DataRoot bri : Ri)
    {
        for (DataRoot brcom : complemento)
        {
            if (bri.getMesa().equals(brcom.getMesa()))
            {
                if (found) {found = false; break;}
                postrecom = brcom.getOrden().getPostres();
                Calendar dateri = Calendar.getInstance();
                Calendar datecom = Calendar.getInstance();
                dateri.setTime(bri.getFechaorden());

                for (Postres proc : postrecom)
                {
                   // if (found) {found = false; break;}
                    datecom.setTime(proc.getHora_plato());
                    long diff = datecom.getTimeInMillis() - dateri.getTimeInMillis();

                    if ( diff > (3*60*1000))
                    {
                        found = true;
                        Ri.add(brcom);
                        break;

                    }
                    else
                    {
                        bri.getOrden().getPostres().add(proc);
                        setBase();

                    }
                }
            }
        }
        }

As you can notice, if some conditions are met, the Array "Ri" which is the main array will increase its content, lets say from 3 items to 4, at the start, the loop was going to be from 0 to 2 but as it got a new element I need it to keep running from 0 to 3 but the range will not dynamically increase as new items are added. 如您所见,如果满足某些条件,则作为主数组的数组“ Ri”将增加其内容,比如说从3项增加到4,一开始循环将是从0到2,但是它有一个新元素,我需要它从0一直运行到3,但是范围不会随着添加新项目而动态增加。

I could count how many items I added to "Ri" So that I can call that many times this method but if I do so, the compiler witll give me the "java.util.ConcurrentModificationException" error at this point I dont know what to do any help would be appreciated. 我可以算出我添加到“ Ri”的项目的数量,这样我就可以多次调用此方法,但是如果这样做,编译器将在此时给我“ java.util.ConcurrentModificationException”错误,我不知道该怎么办。做任何帮助将不胜感激。

I might be wrong, but from the explanation you have given and code you have written, you are trying to get complement Items and then add them to Ri list if not present. 我可能是错的,但是从您给出的解释和编写的代码来看,您试图获取complement项目,然后将它们添加到Ri列表(如果不存在)。

Assuming data structure of Ri is ArrayList, the way I would approach is that, first I would get all the complement items. 假设Ri的数据结构是ArrayList,我将采用的方法是,首先获取所有complement项。 Then loop through complement items, and for each complement item, loop through the entire Ri value and set a boolean if the associated value is suitable to add like below : 然后遍历补码项,对于每个补码项,遍历整个Ri值,如果相关值适合添加,则设置一个布尔值,如下所示:

for(dataBri brCom : complemento) {
   boolean shouldAdd = false;
    for (DataBri bri : Ri) {
     if(someConditionMet) {
       shouldAdd = true;
    }
     if (shouldAdd) {
       Ri.add(brCom);
     }
}

I hope it makes sense. 我希望这是有道理的。

Well im relatively new developing in Java and seems like if you do loop type For-each like 好吧,即时通讯在Java中是一个相对较新的开发项目,好像您执行循环类型For-each一样

for (Arraylist arraylist : size)
   {
      ....
   }

assuming your initial size was 2, meaning that the array contains 2 elements, IF in the process you add elements to the array, the size wont dynamically change BUT if you go for the old 假设您的初始大小为2,这意味着该数组包含2个元素,如果在向数组中添加元素的过程中,如果您选择的是旧大小,则大小不会动态更改

for (int i = 0; i < Arraylist.size(); i++)
    {
      ....
    }

and in the process you add elements to the Array, the size of the loop gets re-calculated dynamically as you add new elements to the array and in my case, solving my problem. 在向数组中添加元素的过程中,在向数组中添加新元素时动态地重新计算了循环的大小,以我为例,这解决了我的问题。

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

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