how can I split a list(e.g. 24 entries) into several parts(each containing max. 10 entries) and cover the last entries?
What I have done so far:
//init list
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 24; i++) {
list.add(i);
}
List<Integer> splitted = new ArrayList<Integer>();
for (int i = 0; i < list.size(); i++) {
splitted.add(list.get(i));
if(i != 0 && i % 10开发者_StackOverflow == 0) {
//print splitted list
System.out.println(splitted);
splitted.clear();
}
}
That works for the entries 0-20 but the entries 21,22,23 and 24 will not appear in any list. How can I fix this? Because i % 10 == 0
won't work for them.
This should work or at leart give you some idea. What I do is first check whether list is finished or splitted reached 10. Then check if i is not equal to 0 and print splitted.
for (int i = 0; i < list.size(); i++) {
splitted.add(list.get(i));
if(i==list.size()-1 || i%10==0) {
if(i!=0){
//print splitted list
System.out.println(splitted);
splitted.clear();
}
}
}
精彩评论