3

I dont understand why continue causes error here

public void clear() {

    log.debug("Clearing hash");

    // wow!
    while( hash.size()>0 ) {

        for(Map.Entry<Node,Node> entry : hash.entrySet()) {

            clearingParents: {

                while( entry.getKey().ups.size() > 0 ) {

                    for(Node node : entry.getKey().ups) {

                        log.debug("Clearing {}, hash size is {}", node, hash.size());
                        if( node.sizeUps() == 0 ) {
                            node.clear();
                            continue clearingParents;
                        }
                        else {
                            log.debug("was skipped since inserted");
                        }
                    }

                    break clearingParents;
                }
            }

        }


    }

I am using this scheme since node.clear() causes iterator to appear broken

Suzan Cioc
  • 29,281
  • 63
  • 213
  • 385
  • 1
    Nooooooo!!!!! This code is cursed! Cyclomatic complexity and labels, have you no respect for Dijkstra?!? – rees Oct 31 '12 at 13:07
  • I'd like to add this for anyone finding this question: **Make sure you aren't working inside of a lambda within the loop. To get out of the lambda, just use `return;`** – Kerwin Sneijders Mar 10 '20 at 10:05

1 Answers1

5

you label an block instead of the while loop. you can break out of a labeled block but not continue the labeled block it doesnt make sense to continue a labeled block as it aint a loop label your loop like below

  clearingParents:  while( entry.getKey().ups.size() > 0 ) {

                        for(Node node : entry.getKey().ups) {

                            log.debug("Clearing {}, hash size is {}", node, hash.size());
                            if( node.sizeUps() == 0 ) {
                                node.clear();
                                continue clearingParents;
                            }
                            else {
                                log.debug("was skipped since inserted");
                            }
                        }

                        break clearingParents;
                    }

study about labeling loops here

PermGenError
  • 45,977
  • 8
  • 87
  • 106