hacks4life
hacks4life

Reputation: 693

How to refactor code in order to avoid label in a Java loop?

How can I refactor my code to remove the label and the need for it ? I am aware that a loop with label is "forbidden". But I can not find a way to rewrite this without the label.

Any ideas ? Thanks

private List<myList> sortLinks(SegmentType s, Set<myList> LinkSet) {
        List<myList> LinkList = new LinkedList<myList>();

        String dep = s.getDep().toString();
        mainLoop: for (int index = 0; !LinkSet.isEmpty(); index++) {

            for (Iterator<myList> iterator = LinkSet.iterator(); iterator.hasNext(); ) {
                myList link = iterator.next();
                if (link.getLegDep().toString().equals(dep)) {
                    iterator.remove();
                    link.setLine(s.getLineCode());
                    link.setNb(s.getNb());
                    link.setSuff(s.getSuff());
                    link.setIndex(index);
                    linkList.add(link);

                    dep = link.getDest().toString();
                    continue mainLoop;
                }
            }

            return Collections.emptyList();
        }
        return linkList;
    }

Upvotes: 1

Views: 1994

Answers (3)

Ivan T
Ivan T

Reputation: 71

Actually eurythmia answer is kind of correct. You can use boolean value to continue like this:

private List<myList> sortLinks(SegmentType s, Set<myList> LinkSet) {
    List<myList> LinkList = new LinkedList<myList>();

    String dep = s.getDep().toString();
    for (int index = 0; !LinkSet.isEmpty(); index++) {

        for (Iterator<myList> iterator = LinkSet.iterator(); iterator.hasNext(); ) {
            myList link = iterator.next();
            boolean toContinue = false;
            if (link.getLegDep().toString().equals(dep)) {
                iterator.remove();
                link.setLine(s.getLineCode());
                link.setNb(s.getNb());
                link.setSuff(s.getSuff());
                link.setIndex(index);
                linkList.add(link);

                dep = link.getDest().toString();
                toContinue = true;
                break; // this breaks inner loop
            }
        }
        if(toContinue) {
            continue; // this continues main loop without executing the 'return'
        }
        return Collections.emptyList();
    }
    return linkList;
}

Upvotes: 1

ajb
ajb

Reputation: 31689

You will probably need a boolean. I don't completely understand what your code is trying to accomplish, but it looks like the inner loop is trying to find a "dependency" of some kind? If that's the case, I'd name the boolean depedencyFound; if I've really guessed wrong, you can call it something else. Anyway, something like this:

private List<myList> sortLinks(SegmentType s, Set<myList> LinkSet) {
    List<myList> LinkList = new LinkedList<myList>();

    String dep = s.getDep().toString();
    for (int index = 0; !LinkSet.isEmpty(); index++) {

        boolean dependencyFound = false;
        for (Iterator<myList> iterator = LinkSet.iterator(); iterator.hasNext(); ) {
            myList link = iterator.next();
            if (link.getLegDep().toString().equals(dep)) {
                iterator.remove();
                link.setLine(s.getLineCode());
                link.setNb(s.getNb());
                link.setSuff(s.getSuff());
                link.setIndex(index);
                linkList.add(link);

                dep = link.getDest().toString();
                dependencyFound = true;
                break;
            }
        }

        if (!dependencyFound) {
            return Collections.emptyList();
        }
    }
    return linkList;
}

If you name the variable right, it will make it pretty clear what's going on. Here, you look for a dependency; if you don't find one, you return an empty list; if you do, then you iterate again through the main loop.

P.S. Once you've done that, then it may be fairly easy to take the whole inner loop and move it to a method that returns a boolean, although it looks like it will also need to return a String with the new value of dep. But if you find a way to do this, it will clean up your code even more.

Upvotes: 2

Ch4ni
Ch4ni

Reputation: 312

since the continue is at the end of the inner loop, simply using break should start the next iteration of the main loop.

Upvotes: -1

Related Questions