1. 程式人生 > >分頁算法

分頁算法

swing pan 遍歷數組 偏移 imp ack 分頁 版本 set

 1 package 分頁算法;
 2 
 3 import javax.swing.border.EmptyBorder;
 4 
 5 public class 分頁版本1 {
 6     public static void main(String[] args) {
 7         算法(8, 15);
 8     }
 9 
10     public static void 算法(int CurPage, int endPage) {
11 
12         int index[] = new int[9];
13         if (endPage > 9) {
14 int nextPage; 15 if (CurPage <= 5) { 16 for (int i = 1; i <= 5; i++) 17 index[i - 1] = i; 18 nextPage = 6; 19 } else { 20 index[0] = 1; 21 index[1] = 2; 22 index[2] = -1;
23 index[3] = CurPage - 1; 24 index[4] = CurPage; 25 nextPage = CurPage + 1; 26 } 27 28 if (endPage - CurPage < 5) { 29 int j = 5; 30 for (int i = CurPage; i < endPage; i++, j++) 31 index[j] = nextPage++;
32 int offset = 4 - endPage + CurPage;// 偏移量 33 34 for (int ii = 0; ii < offset; ii++) {// 數組偏移 35 for (int i = j; i > 2 + ii; i--) { 36 index[i] = index[i - 1]; 37 } 38 j++; 39 } 40 // 數組重構前段 41 int label = (int) Math.ceil((double) offset / 2); 42 for (int i = 0; i < label; i++) { 43 index[2 + i] = 3 + i; 44 } 45 index[label + 2] = -1; 46 for (int i = 8; i > label + 2; i--) { 47 index[i] = endPage--; 48 } 49 50 } else { 51 index[5] = nextPage; 52 index[6] = -1; 53 index[7] = endPage - 1; 54 index[8] = endPage; 55 } 56 } else { 57 for (int i = 0; i < endPage; i++) 58 index[i] = i + 1; 59 } 60 // 遍歷數組 61 int end = endPage > 9 ? 9 : endPage; 62 for (int i = 0; i < end; i++) { 63 System.out.println(index[i]); 64 } 65 } 66 }

分頁算法