如何避免重复以下代码?

use*_*499 3 java

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);
}
Run Code Online (Sandbox Code Playgroud)

我在复制循环之前和循环内的处理代码感觉不对.如何避免重复此代码?

mel*_*okb 6

正如评论中指出的那样,使用do...while循环代替:

List<Widget> widgets;
int offset = 0;
do {
    widgets = widgetDao.retrieveWidgets(widgetBatchSize, offset);
    createWidgets(widgets);
    offset += widgetBatchSize;
} while (widgets.size() >= widgetBatchSize);
Run Code Online (Sandbox Code Playgroud)