Ign*_*rat 18 java arrays collections list
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;
}
Run Code Online (Sandbox Code Playgroud)
嗨,我试图将一个集合划分为更小的集合,这取决于"母亲"集合中的项目数量,并在每次调用get方法时返回其中一个较小的集合(跟踪它是哪一个),有人可以帮我一把吗?
非常感谢你.
伊格纳西奥
Thu*_*fir 42
也许我不明白这个问题,但这是List的一部分:
List<E> subList(int fromIndex, int toIndex)
Run Code Online (Sandbox Code Playgroud)
返回指定fromIndex(包含)和toIndex(独占)之间此列表部分的视图.(如果fromIndex和toIndex相等,则返回的列表为空.)返回的列表由此列表支持,因此返回列表中的非结构更改将反映在此列表中,反之亦然.返回的列表支持此列表支持的所有可选列表操作.
此方法消除了对显式范围操作(对于数组通常存在的排序)的需要.任何需要列表的操作都可以通过传递subList视图而不是整个列表来用作范围操作.例如,以下习语从列表中删除了一系列元素:
list.subList(from, to).clear();
docs.oracle.com/javase/1.5.0/docs/api/java/util/List.html
private int runs = 0;
public void setRunsOneMore() {
runs++;
}
public void setRunsOneLess() {
runs--;
}
public Collection<Comment> getCommentCollection() {
commentCollection = movie.getCommentCollection();
Collection[] com = split((List<Comment>) commentCollection,4);
try{
return com[runs];
} catch(ArrayIndexOutOfBoundsException e) {
runs = 0;
}
return com[runs];
}
public Collection[] 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);
}
return batches;
}
Run Code Online (Sandbox Code Playgroud)
使用下一个和上一个按钮操作设置当前“运行”
public String userNext() {
userReset(false);
getUserPagingInfo().nextPage();
movieController.setRunsOneMore();
return "user_movie_detail";
}
public String userPrev() {
userReset(false);
getUserPagingInfo().previousPage();
movieController.setRunsOneLess();
return "user_movie_detail";
}
Run Code Online (Sandbox Code Playgroud)