免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1220 | 回复: 0
打印 上一主题 下一主题

一个去数组索引位置的程序 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-04-20 23:22 |只看该作者 |倒序浏览

import java.util.HashMap;
import java.util.Map;
@SuppressWarnings("unchecked")
public class PageTable {
    public PageTable() {
        db.put("month1", month1);
        db.put("month2", month2);
        db.put("month3", month3);
        db.put("month4", month4);
        db.put("month5", month5);
        db.put("month6", month6);
        db.put("month7", month7);
        db.put("month8", month8);
        db.put("month9", month9);
        db.put("month10", month10);
        db.put("month11", month11);
        db.put("month12", month12);
    }
    public Map db = new HashMap();
    // 测试数据
    public int[] month1 = { 1, 2, 3, 4, 5 };
    public int[] month2 = { 1, 2, 3 };
    public int[] month3 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
    public int[] month4 = { 1, 2, 3, 4 };
    public int[] month5 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
    public int[] month6 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
    public int[] month7 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
    public int[] month8 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
    public int[] month9 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
    public int[] month10 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
    public int[] month11 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
    public int[] month12 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
    // 开始表
    public static int startTable = 0;
    // 结束表
    public static int stopTable = 0;
    // 每张表索引返回值
    public static int[][] tableIndex;
    /**
     * @param startMonth
     * 开始月
     * @param stopMonth
     * 结束月
     * @param pageNum
     * 每页显示数
     * @param start
     * 开始记录数
     * 参数返回一个二维数组,第一位为表名,第2位为开始索引,第3位为结束索引
     */
    public int[][] getTables(int startMonth, int stopMonth, int pageNum,
            int start) {
        // 返回值
        int startIndex = -1;
        // 取各个表的总数
        int[] intArray = null;
        int[] countArray = new int[stopMonth - startMonth + 1];
        int totalCount = 0;
        // 只计算一次各个月表的总数
        for (int i = 0; i  stopMonth - startMonth + 1; i++) {
            intArray = (int[]) db.get("month" + (i + startMonth));
            int count = intArray.length;
            countArray = count;
            totalCount += count;
        }
        if (start > totalCount) {
            return new int[0][0];
        }
        // 一共有2中情况获取数据
        // 1在一张表可以获取
        // 2在相邻的多张表获取数据
        // 确定可以获取数据的表
        int subCount = 0;
        for (int i = 0; i  countArray.length; i++) {
            subCount += countArray;
            // 从大于开始记录数的表开始计算
            if (subCount >= start) {
                // 索引值与第一张表的确定
                if (startIndex  0) {
                    startIndex = (countArray - (subCount - start));
                    startTable = i + startMonth;
                }
                // 一张表里面可以查询所有数据
                if (subCount >= (start + pageNum)
                        && ((subCount - countArray)  start)) {
                    // 记录开始表和结束表
                    stopTable = i + startMonth;
                    break;
                }
                // 相邻多张表
                if (subCount >= (start + pageNum)
                        && ((subCount - countArray)  (start + pageNum))) {
                    stopTable = i + startMonth;
                    break;
                }
                if (i == countArray.length - 1) {
                    // 记录结束表
                    stopTable = i + startMonth;
                }
            }
        }
        // 构建各个表的索引
        tableIndex = new int[stopTable - startTable + 1][3];
        for (int i = 0; i  stopTable - startTable + 1; i++) {
            // 查询的表
            tableIndex[0] = startTable + i;
            // 开始数据的索引
            if (i == 0) {
                tableIndex[1] = startIndex;
            } else {
                tableIndex[1] = 0;
            }
            // 结束数据的索引
            if (countArray[startTable - startMonth + 1 + i] >= (pageNum + startIndex)) {
                tableIndex[2] = pageNum + startIndex;
            } else {
                if(istopTable - startTable){
                    tableIndex[2] = countArray[startTable - startMonth + 1 + i];
                    pageNum = pageNum -countArray[startTable - startMonth + 1 + i];
                }else{
                    tableIndex[2] = pageNum;
                }
            }
        }
        return tableIndex;
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
        PageTable pi = new PageTable();
        int[][] retVal = pi.getTables(3, 6, 10, 11);
        System.out.println(startTable);
        System.out.println(stopTable);
        for (int i = 0; i  retVal.length; i++) {
            for (int j = 0; j  retVal.length; j++) {
                System.out.print(retVal[j]);
                System.out.print(" ");
            }
            System.out.println();
        }
    }
}


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/17391/showart_1904929.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP