【发布时间】:2014-05-28 19:38:41
【问题描述】:
我想以小批量迭代 ArrayList。
例如,如果 ArrayList 大小为 75,batch 大小为 10,我希望它处理记录 0-10,然后是 10-20,然后是 20-30,等等。
我试过了,但是没用:
int batchSize = 10;
int start = 0;
int end = batchSize;
for(int counter = start ; counter < end ; counter ++)
{
if (start > list.size())
{
System.out.println("breaking");
break;
}
System.out.println("counter " + counter);
start = start + batchSize;
end = end + batchSize;
}
【问题讨论】:
-
在什么情况下不起作用?
-
计数器变量没有正确的值。输出为:计数器 0 计数器 1 计数器 2 计数器 3 计数器 4 计数器 5 计数器 6 断开
-
这与您期望看到的有何不同?我们无法读懂您的想法,也不知道您在
list中拥有什么数据。 -
我创建的arraylist包含随机数量的String元素[总共63个元素]。第一次迭代中的计数器值为 0,这是正确的。但我假设在随后的迭代中值应该是 10、20 等。
标签: java for-loop arraylist batch-processing