类似淘宝分页算法

package com.lyis.test;

public class Page {
	
	public void test(int pageCount, int pageNo) {
		int pageSize = 6;
		int start = 0; 
		int end = 0;
		if (pageNo < pageSize) {
			start = 1;
			end = pageSize;
		} else {
			start = pageNo - 2;
			end = pageNo + 2;
		}
		if (end > pageCount) {
			end = pageCount;
		}		
		if (pageNo > 1) {
			System.out.print("[prev]");
		}
		if (start > 1) {
			System.out.print("[1]");
			System.out.print("...");
		}
		for (int i = start; i <= end; i++) {
			if (i == pageNo) {
				System.out.print("[#" + i + "#]");
			} else {
				System.out.print("[" + i + "]");
			}
		}			
		if (end < pageCount) {
			if (end != pageCount - 1) {
				System.out.print("...");
			}
			System.out.print("[" + pageCount + "]");
		}
		if (pageNo < pageCount) {
			System.out.print("[next]");
		}
		System.out.println("\n");
	}
	
	public static void main(String[] args) {
		Page page = new Page();
		int pageCount = 20;
		for (int i = 1; i <= pageCount; i++) {
			page.test(pageCount, i);
		}
	}
}


你可能感兴趣的:(类似淘宝分页算法)