赞
踩
package com;
import org.apache.commons.lang3.ObjectUtils;
import java.util.ArrayList;
import java.util.List;
public class ListUtils {
public static <T> List<T> getList(List<T> list, int pageIndex, int pageSize){
List<T> lists = new ArrayList<>();
if(ObjectUtils.isNotEmpty(list)){
int startIndex;
int endIndex;
int total = list.size();
int num = total%pageSize;
startIndex = (pageIndex - 1)*pageSize;
if(startIndex > total){
return new ArrayList<>();
}
endIndex = pageIndex * pageSize;
if(endIndex > total) {
endIndex = (pageIndex - 1)*pageSize + num;
}
lists = list.subList(startIndex, endIndex);
}
return lists;
}
}
测试下
import com.ListUtils;
import com.google.common.collect.Lists;
import java.util.List;
public class HelloWord {
public static void main(String[] args) {
List<Integer> list = Lists.newArrayList(1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9);
List<Integer> lists = ListUtils.getList(list, 3, 10);
System.out.println(lists);
}
}
控制台打印
[3, 4, 5, 6, 7, 8, 9]
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。