简体   繁体   中英

Java : skip nested loops

I know i can skip loops in java from inner to outer this way:

loop1:
for (Object o : objects1){
   loop2:
   for (Object o : objects2){
       if (o.getSomething()) continue loop1 ;
   }
}

But how could I go the other way round, that is checking from outside if a loop is executed:

loop1:
for (Object o : objects1){
   if (condition) continue loop3;
   loop2:
   for (Object o : objects2){
       loop3:
       for (Object o : objects2){

       }
   }
}

if (condition) continue loop3; obviousely won't work, but you get the idea.

Why would I want to do this? I check every single combination of items with a lot of nested loops. Within the loops conditions are created. Based on these conditions not always all successing loops are necessary, thus skipping them would increase performance greatly.

Any ideas?

Trying to be more clear:

ItemSet<Item> set = new ItemSet<>();

for (Item a : itemtype1){
   for (Item b: itemtype2){
      set.add(a);
      set.add(b);
      if (set.getX() == 1) "execute from loop1 on"
      if (set.getX() == 2) "execute from loop2 on"

      loop1:
      for (Item c : itemtype3){
          loop2:
          for (Item d : itemtype4){
              //execute final code here - this must be reached in any case
          }
      }
   }
}

Make loop3 a method and call it when needed, ex:

void optimizedFunction(){
    loop1:
    for (Object o : objects1){
       if (condition) { loop3(objects2); continue;}
       loop2:
       for (Object o : objects2){
           loop3(objects2)
       }
    }
}



void loop3(List objects2){
       for (Object o : objects2){

       }
}

Your question does not make much sense. Either a continue/break solves your problem or it can't. It's unclear what you really want to do.

Using a while-loop or an explicit for-loop instead will give you exact control about what is happening, for example if you have found an object in the inner loop that is the only one you are insterested in you can do something like this:

int iMin = 0;
int jMin = 0;
int iMax = object1.size();
int jMax = object2.size();

for(int i = iMin; i < iMax; i++) {
   for(int j = jMin; j < jMax; j++) {
      if(object1.get(i).someComparison(object2.get(j))) {
          jMin = i;
          jMax = i+1;
          break; //this will break the inner loop and continue the outer one
      }
   }
}

on the other hand you can always access a condition that was declared in an outer scope in an inner one so there is no reason why you would want to put a continue out of the loop you are trying to continue.

If your problem is that setting some condition inside inner loops won't allow your outer loops to see it, you can define the condition outside all loops to make it global for all loops.
If not please specify what are you want to achieve.

boolean condition;
loop1:
for (Object o : objects1){
   if (condition) continue loop3;
   loop2:
   for (Object o : objects2){
       loop3:
       for (Object o : objects2){
            if (something_trivial()){
                condition = false;
                continue loop1;
            }
       }
   }
}

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