List<Widget> widgets = widgetDao.retrieveWidgets(widgetBatchSize, 0);
createWidgets(widgets);
for (int offset = widgetBatchSize; widgets.size() >= widgetBatchSize;
offset += widgetBatchSize) {
widgets = widgetDao.retrieveWidgets(widgetBatchSize, offset);
createWidgets(widgets);
}
我复制循环之前和循环内的处理代码感觉不对。如何避免重复此代码?
答案 0 :(得分:6)
正如评论中所指出的,改为使用do...while
循环:
List<Widget> widgets;
int offset = 0;
do {
widgets = widgetDao.retrieveWidgets(widgetBatchSize, offset);
createWidgets(widgets);
offset += widgetBatchSize;
} while (widgets.size() >= widgetBatchSize);