重构 - 简化 Java 中的嵌套 for 循环 [英] Refactoring - Simplifying nested for loops in java

查看:58
本文介绍了重构 - 简化 Java 中的嵌套 for 循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要弄清楚如何改进以下代码:

I need to figure out how to improve following code:

      for (DirCategory c1 : categories1) {
            c1.setCount(dirEntryService.getDirEntryCategoryCount(c1));
            log.debug("c1: "+c1.getCount()+" - "+c1.getName());
            dirCategoryService.persist(c1);

            List<DirCategory> categories2 = c1.getChildren();
            for (DirCategory c2 : categories2) {
                c2.setCount(dirEntryService.getDirEntryCategoryCount(c2));
                log.debug("  c2: "+c2.getCount()+" - "+c2.getName());
                dirCategoryService.persist(c2);

                List<DirCategory> categories3 = c2.getChildren();
                for (DirCategory c3 : categories3) {
                    c3.setCount(dirEntryService.getDirEntryCategoryCount(c3));
                    log.debug("    c3: "+c3.getCount()+" - "+c3.getName());
                    dirCategoryService.persist(c3);

                    List<DirCategory> categories4 = c3.getChildren();
                    for (DirCategory c4 : categories4) {
                        c4.setCount(dirEntryService.getDirEntryCategoryCount(c4));
                        log.debug("      c4: "+c4.getCount()+" - "+c4.getName());
                        dirCategoryService.persist(c4);

                        List<DirCategory> categories5 = c4.getChildren();
                        for (DirCategory c5 : categories5) {
                            c5.setCount(dirEntryService.getDirEntryCategoryCount(c5));
                            log.debug("        c5: "+c5.getCount()+" - "+c5.getName());
                            dirCategoryService.persist(c5);

                            List<DirCategory> categories6 = c5.getChildren();
                            for (DirCategory c6 : categories6) {
                                 c6.setCount(dirEntryService.getDirEntryCategoryCount(c6));
                                log.debug("          c6: "+c6.getCount()+" - "+c6.getName());
                                 dirCategoryService.persist(c6);
                            }
                        }
                    }
                }
            }
        }

我真的很感激任何帮助简化这个东西"

I would really appreciate any help simplifying this "thing"

推荐答案

这对于递归来说看起来很不错,因为所有循环都具有完全相同的结构和内容.递归思想是将所有循环嵌套到某个深度 d,递归结构为

This looks like a great job for recursion, since all of the loops have exactly the same structure and content. The recursive idea is to nest all the loops to some depth d, with the recursive structure being

  • 嵌套到深度为零是无操作的,并且
  • 嵌套到深度 d + 1 会对所有深度为 d 的循环执行 for 循环.

可以写成

private static void recursiveExplore(List<DirCategory> categories, int depth) {
    if (depth == 0) return;

    for (DirCategory c1 : categories) {
        c1.setCount(dirEntryService.getDirEntryCategoryCount(c1));
        log.debug("c1: "+c1.getCount()+" - "+c1.getName());
        dirCategoryService.persist(c1);

        recursiveExplore(c1.getChildren(), depth - 1);
    }
}
public static void explore(List<DirCategory> categories) {
    recursiveExplore(categories, 5);
}

然后您可以通过调用 explore 进行探索.

You can then do the exploration by calling explore.

当然,这种方法适用于深度最多为 5 的假设.如果您想消除深度要求并一直探索到目录底部,那么您可以像这样消除深度参数:

Of course, this approach works with the assumption that the depth is at most five. If you want to eliminate the depth requirement and just explore all the way down to the bottom of the directory, then you can just eliminate the depth parameter like this:

public static void explore(List<DirCategory> categories) {
    for (DirCategory c1 : categories) {
        c1.setCount(dirEntryService.getDirEntryCategoryCount(c1));
        log.debug("c1: "+c1.getCount()+" - "+c1.getName());
        dirCategoryService.persist(c1);

        recursiveExplore(c1.getChildren(), depth - 1);
    }
}

更一般地说,任何时候您想在彼此内部嵌套任意数量的循环,都可以考虑将递归作为一种选择.这是表达这个概念的一个非常通用的框架.

More generally, any time you want to nest an arbitrary number of loops inside of one another, consider recursion as an option. It's a very general framework for expressing this concept.

希望这会有所帮助!

这篇关于重构 - 简化 Java 中的嵌套 for 循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆