import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ListPageUtils<T> {
public static List getListPage(List list, int start, int limit){
List newList = new ArrayList<Object>();
// 将拿到的数据进行分页
int total = list.size();
int fromIndex = 0; // 从哪里开始截取
int toIndex = 0; // 截取几个
if (list != null && list.size() > 0) {
// 当前页小于或等于总页数时执行
if (Integer.valueOf(start) <= total && Integer.valueOf(start) != 0) {
fromIndex = (Integer.valueOf(start) - 1) * Integer.valueOf(limit);
if (Integer.valueOf(start) == total) {
toIndex = list.size();
} else {
toIndex = Integer.valueOf(start) * Integer.valueOf(limit);
}
}
if (list.size() <= toIndex) {
if (fromIndex == 0) {
newList = list.subList(fromIndex, list.size());
} else {
try {
newList = list.subList(fromIndex, list.size());
} catch (Exception e) {
newList = new ArrayList<Object>();
}
}
} else {
newList = list.subList(fromIndex, toIndex);
}
}
return newList;
}
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1,2,33,22,-1,35,67,112,220);
List list2 = ListPageUtils.getListPage(list, 2, 2);
System.out.println(list2);
}
}
List 分页
最新推荐文章于 2024-04-26 16:27:05 发布