处理 Partition<T> 的问题

This commit is contained in:
Boyi C 2022-07-12 17:09:48 +08:00 committed by GitHub
parent a38e3e52af
commit a425ac5fb3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -26,13 +26,14 @@ public class Partition<T> extends AbstractList<List<T>> {
*/ */
public Partition(List<T> list, int size) { public Partition(List<T> list, int size) {
this.list = list; this.list = list;
this.size = Math.min(size, list.size()); this.size = size;
} }
@Override @Override
public List<T> get(int index) { public List<T> get(int index) {
int start = index * size; int listSize = list.size();
int end = Math.min(start + size, list.size()); int start = Math.min(index * size, listSize);
int end = Math.min(start + size, listSize);
return list.subList(start, end); return list.subList(start, end);
} }
@ -41,10 +42,7 @@ public class Partition<T> extends AbstractList<List<T>> {
// 此处采用动态计算以应对list变 // 此处采用动态计算以应对list变
final int size = this.size; final int size = this.size;
final int total = list.size(); final int total = list.size();
int length = total / size; int length = (total + size - 1) / size;
if(total % size > 0){
length += 1;
}
return length; return length;
} }