GOTO Java – Mea Cup O' Jo
Skip to content


GOTO Java

Here’s the scenario:

  1. Loop through the list and for each slot
    1. Compare current value with the given value
    2. If match is found modify current item and break the loop
  2. If the given value wasn’t find in the loop add it to the list

Turns out breaking out of loops using labels also works for curly block of code so the solution is below

List list = getFromSomewhere();
Foo foo = whatever();
gotu: {
    for (Foo f : list) {
        if ( f.equals(foo)) {
            f.setBlah(foo.getBlah()); // blah is not used in compare()
            break gotu;
       }
    }
// only want to execute the following if the loop was never broken
list.add(foo);
}

Posted in Java, Programming.


0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.



Some HTML is OK

or, reply to this post via trackback.