题目:
现有若干条数据,每页显示n条,要求提供一个方法;根据传入的页码数和每页数据条数,输出该页的所有数据;
//例如:Student类(sno,sname,sex,birth,major,score)
public ArrayList<Student> findByPage(int pageNow,int pageSize){
}
代码实现:
学生类:
public class Student {
private String sno;
private String name;
private String major;
private Date birth;
private double score;
public Student() {
}
public Student(String sno, String name, String major, Date birth, double score) {
this.sno = sno;
this.name = name;
this.major = major;
this.birth = birth;
this.score = score;
}
public String getSno() {
return sno;
}
public void setSno(String sno) {
this.sno = sno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
@Override
public String toString() {
return "Student{" +
"sno='" + sno + '\'' +
", name='" + name + '\'' +
", major='" + major + '\'' +
", birth=" + birth +
", score=" + score +
'}';
}
}
实体类:
public class StudentManage {
private List<Student> list;
public StudentManage(List<Student> list) {
this.list = list;
}
/**
*
* @param pageNow 当前的页码数
* @param pageSize 每页显示的数据条数
* @return
*/
public List<Student> findByPage(int pageNow, int pageSize){
pageSize = Math.max(3,pageSize);
//当前页码数不能小于1
pageNow = Math.max(1,pageNow);//最小值为1最大值上不封顶
// pageNow = pageNow < 1 ? 1 : pageNow;
//当前页码数不能超过总页码数
//计算获得总页数(向上取整)
int totalPage = (int) Math.ceil(list.size() / (double) pageSize);
pageNow = Math.min(pageNow,totalPage);
//声明两个临时变量用于表示子集合的截取范围
int start = (pageNow-1)*pageSize;
int end = pageNow*pageSize;
// if (end > list.size()){
// end = list.size();
// }
//起始截取位置不能小于0
start = Math.max(0,start);
//结束的截取位置不能超过总数据条数
end = Math.min(end,list.size());
return list.subList(start,end);
}
}
测试类:
public class TestStudent {
public static void main(String[] args) {
//asList(T... a)
//返回由指定数组支持的固定大小的列表。
List<Student> list = Arrays.asList(
new Student("1001","孔明","挖掘机专修",new Date(),100),
new Student("1002","凤雏","美容美发",new Date(),20),
new Student("1003","卧龙","烹饪",new Date(),104),
new Student("1004","鬼才","IT",new Date(),50),
new Student("1005","虎痴","汽修",new Date(),60),
new Student("1006","庞德","电焊",new Date(),54),
new Student("1007","水镜","餐饮",new Date(),77),
new Student("1008","姜维","铁路管理",new Date(),88),
new Student("1009","玄德","人工智能",new Date(),99),
new Student("1010","诸葛","大数据",new Date(),36)
);
StudentManage sm = new StudentManage(list);
list = sm.findByPage(4,3);
for (Student student : list) {
System.out.println(student);
}
}
}
代码分析:
使用subList方法的很容易截取数据的显示范围,进行分页操作。但没有对页码数的范围和截取的数据显示范围进行判断会产生数组下标越界异常。
通常范围内取值可以使用if…else语句进行判断,或者使用三目运算进行判断;
题目中数据分页,页码数的范围不能小于1且页码数不能超过总的页码数;
同理使用subList方法的截取的数据显示范围,其起始截取位置不能小于0,结束的截取位置不能超过总数据条数
综上,该题使用了Math.max(int a,int b)方法,简化代码
pageNow = Math.max(1,pageNow);
若输入的当前页数小于1则返回1,同理超过总页数返回总页数。