1

I have a method to compare two list of objects.

The objects are unique in both lists.

I doing it with 2 level nested for loops

I want to terminate the inner for loop's remaining cycles if two objects match correctly.

Is it possible to terminate the remaining iteration of a for loop in Java?!

Thanks

The Sample Code:

public class NestedForLoops {

  public static void main(String[] args) {
    String one = "abcdefgh";
    String two = "ijkhmnop";
    System.out.println(nestedForLoop(one, two));

  }

  public static String nestedForLoop(String one, String two)
  {
    String res = "";
    for(int i = 0; i < one.length(); i++)
    {
        for(int j = 0; j < two.length(); j++)
        {
            if(one.charAt(i) == two.charAt(j)){
                System.out.println(i + "   " + j);
                res += one.charAt(i);
                continue;
            }
        }
    }
    return res;
  }

}
2
  • mind showing us what you've done so far with the code? Commented Jan 2, 2011 at 14:52
  • @ thephpdeveloper: I got the answer. I thought break can be only used in if statmetns. I should have gave a try to it. Ok, now i doubt how continue can be useful. Commented Jan 2, 2011 at 15:07

6 Answers 6

3

Break inner:

for (Object o1 : list1)
   for (Object o2 : list2)
        if (o1.equals(o2))
            break;

Break outer for loop:

outer: for (Object o1 : list1)
   for (Object o2 : list2)
        if (o1.equals(o2))
            break outer;
Sign up to request clarification or add additional context in comments.

Comments

1

use the break; statement in the inner loop. If you want to terminate the outer one as well, you'd have to give the outer one a label and use break label;.

Comments

1

Yes it is. Just put an if statement that checks for a match of the two objects, and within the if, put a break statement. Here's the code sample:

while(coinditions for loop 1)
{
    // loop body
    while(conditions for loop 2)
    {
         //loop body

         if(object1.compareTo(object2)==0)
         {
              break;
         }
    }
}

Comments

0

Labeling works (as already replied)

If the method does exactly that, check for equality, then you could also just return from the method when the objects match, looks like a small enough method to not make returning a bad practise. Personally I'd find that more readable than using labels.

Comments

0
public boolean foundEqual(List<Object> list1, List<Object> list2) {
  if (list1 == null || list2 == null) {
    throw new NullPointerException("Lists can not be null");
  }

  for (Object o1 : list1)
   for (Object o2 : list2)
        if (o1.equals(o2)) {
            return true;
        }

  return false;
}

Comments

0

Use a multi-level break:

outside:
for (...)
  for (...) 
    if (found) break outside;

Alternatively, creating a separate method and using return instead of break should be more readable, since return statements are far more common.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.