美文网首页
Java实现非数据库简单分页记录

Java实现非数据库简单分页记录

作者: 爱学习的蹭蹭 | 来源:发表于2023-09-21 14:42 被阅读0次

    Java实现非数据库简单分页记录 此demo,适合抓取网页文章。

    模拟数据进行测试

    
    import java.util.ArrayList;
    import java.util.List;
    public class DataPagination {
        public static void main(String[] args) {
            // 假设我们有72条数据
            List<String> data = new ArrayList<>();
            for (int i = 1; i <= 72; i++) {
                data.add("Data " + i);
            }
            // 每页的数据量
            int pageSize = 10;
            // 计算总页数
            int totalPages = (int) Math.ceil((double) data.size() / pageSize);
            // 输出每一页的数据
            for (int pageNum = 1; pageNum <= totalPages; pageNum++) {
                List<String> pageData = getPage(data, pageNum, pageSize);
                System.out.println("Page " + pageNum + ": " + pageData);
            }
        }
        // 获取指定页数的数据
        public static List<String> getPage(List<String> data, int pageNum, int pageSize) {
            List<String> pageData = new ArrayList<>();
            int startIndex = (pageNum - 1) * pageSize;
            int endIndex = Math.min(startIndex + pageSize, data.size());
            for (int i = startIndex; i < endIndex; i++) {
                pageData.add(data.get(i));
            }
            return pageData;
        }
    }
    

    运行结果

    Page 1: [Data 1, Data 2, Data 3, Data 4, Data 5, Data 6, Data 7, Data 8, Data 9, Data 10]
    Page 2: [Data 11, Data 12, Data 13, Data 14, Data 15, Data 16, Data 17, Data 18, Data 19, Data 20]
    Page 3: [Data 21, Data 22, Data 23, Data 24, Data 25, Data 26, Data 27, Data 28, Data 29, Data 30]
    Page 4: [Data 31, Data 32, Data 33, Data 34, Data 35, Data 36, Data 37, Data 38, Data 39, Data 40]
    Page 5: [Data 41, Data 42, Data 43, Data 44, Data 45, Data 46, Data 47, Data 48, Data 49, Data 50]
    Page 6: [Data 51, Data 52, Data 53, Data 54, Data 55, Data 56, Data 57, Data 58, Data 59, Data 60]
    Page 7: [Data 61, Data 62, Data 63, Data 64, Data 65, Data 66, Data 67, Data 68, Data 69, Data 70]
    Page 8: [Data 71, Data 72]
    
    Process finished with exit code 0
    
    

    Pagination分页实体

    
    public class Pagination {
    
        private int totalRecords; // 总记录数
        private int totalPages; // 总页数
    
        // 构造函数
        public Pagination(int totalRecords, int recordsPerPage) {
            // 整除计算页数,如果有余数则页数加1
            int totalPages = (int) Math.ceil((double) totalRecords / recordsPerPage);
            this.totalRecords = totalRecords;
            this.totalPages = totalPages;
        }
    
        // 获取总记录数
        public int getTotalRecords() {
            return totalRecords;
        }
    
        // 获取总页数
        public int getTotalPages() {
            return totalPages;
        }
    
    
    }
    
    

    运行类

    public class Test {
        public static void main(String[] args) {
       // 总记录数为102,每页显示20条记录
            Pagination pagination = new Pagination(102, 20);
    
            System.out.println("Total records: " + pagination.getTotalRecords()); // 总记录数
            System.out.println("Total pages: " + pagination.getTotalPages()); // 总页数
    
        }
    
    
    }
    

    运行结果

    Total records: 102
    Total pages: 6
    

    相关文章

      网友评论

          本文标题:Java实现非数据库简单分页记录

          本文链接:https://www.haomeiwen.com/subject/xorzvdtx.html