[英]How to limit the number of iterations through the List
How I can limit the result of an iterator over my List? 我如何限制迭代器的结果超过我的列表?
try {
ArrayList<CalanderQueryOutput> results = new ArrayList<CalanderQueryOutput>();
List<?> eventsToday = (List<?>) filter.filter(calendar.getComponents(Component.VEVENT));
CalanderQueryOutput caldavOutput = new CalanderQueryOutput();
for (Iterator<?> i = eventsToday.iterator(); i.hasNext();) {
}
results.add(caldavOutput);
}
I want list only maximum ten results 我想列出最多十个结果
You can add a counter and break;
你可以添加一个计数器并
break;
the loop when the counter reaches a certain value. 计数器达到某个值时的循环。
Before the loop: 在循环之前:
int counter = 1;
in the loop: 在循环:
if(counter >= 10)
break;
/* loop code */
counter++;
Just do this: 这样做:
...
for (Object event : eventsToday.subList(0, Math.min(9, eventsToday.size() - 1))) {
// do something with "event"
}
...
In the loop , use a counter and check it like this: 在循环中,使用计数器并检查如下:
if(counter < 10) results.add(caldavOutput);
.....
......
counter++;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.