方法一:
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
list.add(123);
list.add(234);
list.add(345);
list.add(456);
list.add(567);
list.add(678);
list.add(789);
list.add(890);
list.add(901);
System.out.println(getPage(list, 1, 2));
System.out.println(getPage(list, 2, 2));
System.out.println(getPage(list, 3, 2));
System.out.println(getPage(list, 4, 2));
System.out.println(getPage(list, 5, 2));
System.out.println(getPage(list, 6, 2));
System.out.println();
System.out.println(getPage(list, 1, 3));
System.out.println(getPage(list, 2, 3));
System.out.println(getPage(list, 3, 3));
System.out.println(getPage(list, 4, 3));
System.out.println();
System.out.println(getPage(list, 1, 4));
System.out.println(getPage(list, 2, 4));
System.out.println(getPage(list, 3, 4));
System.out.println(getPage(list, 4, 4));
System.out.println();
System.out.println(getPage(list, 1, 5));
System.out.println(getPage(list, 2, 5));
System.out.println(getPage(list, 3, 5));
System.out.println(getPage(list, 4, 5));
}
public static <T> List<T> getPage(List<T> list, int pageNum, int pageSize) {
int fromIndex = (pageNum - 1) * pageSize;
if (fromIndex >= list.size()) {
return Collections.emptyList();
}
int toIndex = Math.min(fromIndex + pageSize, list.size());
return list.subList(fromIndex, toIndex);
}