我需要弄清楚如何改進下面的代碼簡化:重構 - 嵌套在Java循環
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);
}
}
}
}
}
}
我真的很感激這個簡化的「東西」
OMG多麼瘋狂的代碼... – smas 2011-03-22 22:44:11
我的大腦都在牆上。 – 2011-03-22 23:09:25