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 }
js版:
1 function rotate(CurPage, endPage){ 2 var index = new Array(); 3 if(endPage > 9) { 4 var nextPage; 5 if( CurPage<= 5) { 6 for(var i = 1; i <= 5; i++) index[i - 1] = i; 7 nextPage = 6; 8 } else { 9 index[0] = 1; 10 index[1] = 2; 11 index[2] = -1; 12 index[3] = CurPage - 1; 13 index[4] = CurPage; 14 nextPage = CurPage + 1; 15 } 16 if(endPage - CurPage < 5) { 17 var j = 5; 18 for(var i = CurPage; i < endPage; i++, j++) index[j] = nextPage++; 19 var offset = 4 - endPage + CurPage; // 偏移量 20 for(var ii = 0; ii < offset; ii++) { // 数组偏移 21 for(var i = j; i > 2 + ii; i--) { 22 index[i] = index[i - 1]; 23 } 24 j++; 25 } 26 // 数组重构前段 27 var label = Math.ceil(offset / 2); 28 for(var i = 0; i < label; i++) { 29 index[2 + i] = 3 + i; 30 } 31 index[label + 2] = -1; 32 for(var i = 8; i > label + 2; i--) { 33 index[i] = endPage--; 34 } 35 } else { 36 index[5] = nextPage; 37 index[6] = -1; 38 index[7] = endPage - 1; 39 index[8] = endPage; 40 } 41 } else { 42 for(var i = 0; i < endPage; i++) index[i] = i + 1; 43 } 44 // 遍历数组 45 var end = endPage > 9 ? 9 : endPage; 46 for(var i = 0; i < end; i++) { 47 console.info(index[i]) 48 } 49 }