【发布时间】:2025-12-26 10:55:16
【问题描述】:
public Collection<Comment> getCommentCollection() {
commentCollection = movie.getCommentCollection();
return split((List<Comment>) commentCollection, 4);
}
public Collection<Comment> split(List<Comment> list, int size){
int numBatches = (list.size() / size) + 1;
Collection[] batches = new Collection[numBatches];
Collection<Comment> set = commentCollection;
for(int index = 0; index < numBatches; index++) {
int count = index + 1;
int fromIndex = Math.max(((count - 1) * size), 0);
int toIndex = Math.min((count * size), list.size());
batches[index] = list.subList(fromIndex, toIndex);
set = batches[index];
}
return set;
}
我正在尝试将较大的集合拆分为较小的集合,具体取决于原始集合中的项目数。然后在每次调用 get 方法时返回较小的集合之一,同时跟踪返回的是哪个较小的集合。我怎样才能做到这一点?
【问题讨论】:
-
ArrayList 优于 Vector。
-
没有与问题的直接链接,但您应该使用 Vector (ArrayList?) 以外的其他东西 => *.com/questions/300519/…
标签: java arrays list collections