简体   繁体   中英

Dynamically change for-loop range in Java

Is there a way to increase the for-loop range in java? 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.

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.

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.

Assuming data structure of Ri is ArrayList, the way I would approach is that, first I would get all the complement items. 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 :

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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