java问题,求一个比较简单合理的算法。在线等~

说明:
图1是数据库返回的结果集示例,图2是页面上需要显示的最终效果
补充:
1,每个游戏在对应的时间只有一个对应的值,不会重复
2,游戏的数量不固定,时间的数量不固定
问题:
如何将图1的结果集转变成另一个结果集方便显示在页面上?

编辑了半天又不显示了 靠.ssssss

 

    public static void main(String[] args){
        //构造数据
        List<Data> list = new ArrayList<Data>();
        list.add(new Data("三国志12", "03-01", "5"));
        list.add(new Data("雷霆战机", "03-01", "2"));
        list.add(new Data("真三国无双7", "03-02", "4"));
        list.add(new Data("三国志12", "03-03", "5"));
        list.add(new Data("雷霆战机", "03-04", "5"));
        list.add(new Data("真三国无双7", "03-05", "5"));
        String[][] tableData = toTable(list);
        if (log.isDebugEnabled()){
            log.debug(JsonUtil.format(tableData));
        }
    }

 

   /**
     * 将 config装成 二维数组 table.
     *
     * @param list
     *            the list
     * @param nameComparator
     *            the name comparator
     * @param keyComparator
     *            the key comparator
     * @return the string[][]
     */
    private static String[][] toTable(List<Data> list){
        //这个问题的核心,是通过name和key 取到 value值
        //因此,为了性能,设计一个map,这个map的key 是config的name和key拼接起来的
        Map<String, Data> map = new LinkedHashMap<String, Data>();
        //LinkedHashSet 保证循环的顺序
        Set<String> nameSet = new LinkedHashSet<String>();
        Set<String> keySet = new LinkedHashSet<String>();
        for (Data config : list){
            String key = config.getKey();
            String name = config.getName();
            nameSet.add(name);
            keySet.add(key);
            //确保map中的key不会重复
            String k = key + "@" + name;
            map.put(k, config);
        }
        int rowLength = list.size();
        int columnLength = nameSet.size() + 1;
        String[][] table = new String[rowLength][columnLength];
        //第一行 放name 标题
        table[0] = CollectionsUtil.toArray(nameSet);
        int i = 0;
        for (String key : keySet){
            String[] array = new String[columnLength];
            array[0] = key;
            int j = 0;
            for (String name : nameSet){
                String k = key + "@" + name;
                Data config = map.get(k);
                String value = null;
                if (null != config){
                    value = config.getValue();
                }
                array[j + 1] = value;
                j++;
            }
            table[i + 1] = array;
            i++;
        }
        return table;
    }

 

13:34:00 DEBUG (CopyOfDataTest.java:90) [main()]     [

                [

            "三国志12",

            "雷霆战机",

            "真三国无双7"

        ],

                [

            "03-01",

            "5",

            "2",

            null

        ],

                [

            "03-02",

            null,

            null,

            "4"

        ],

                [

            "03-03",

            "5",

            null,

            null

        ],

                [

            "03-04",

            null,

            "5",

            null

        ],

                [

            "03-05",

            null,

            null,

            "5"

        ]

    ]

 

 

温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-03-12
查询的时候分一下组就好了
SELECT
tab.`time`,
tab.name,
tab.value
FROM
tab
GROUP BY
tab.`time`,
tab.name追问

查询的结果不可改变,只能通过程序重新组织格式。

追答

那游戏的数目是死的么???还有时间??那个是固定不变的

追问

都是变化的,我提问中有说明。

追答

有笨一点的方法

追问

增加悬赏了,能否贴出你的代码,谢谢

追答

你留个联系方式 我给你说一下我的思路 ,一句两句说不清楚

本回答被网友采纳
第2个回答  2015-03-13
插入排序: package org.rut.util.algorithm.support; import org.rut.util.algorithm.SortUtil; /** * @author treeroot * @since 2006-2-2 * @version 1.0 */ public class InsertSort implements SortUtil.Sort{ /* (non-Javadoc) * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[]) */ public void sort(int[] data) { int temp; for(int i=1;i0)&&(data[j]<data[j-1]);j--){ SortUtil.swap(data,j,j-1); } } } } 冒泡排序: package org.rut.util.algorithm.support; import org.rut.util.algorithm.SortUtil; /** * @author treeroot * @since 2006-2-2 * @version 1.0 */ public class BubbleSort implements SortUtil.Sort{ /* (non-Javadoc) * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[]) */ public void sort(int[] data) { int temp; for(int i=0;ii;j--){ if(data[j]<data[j-1]){ SortUtil.swap(data,j,j-1); } } } } } 选择排序: package org.rut.util.algorithm.support; import org.rut.util.algorithm.SortUtil; /** * @author treeroot * @since 2006-2-2 * @version 1.0 */ public class SelectionSort implements SortUtil.Sort { /* * (non-Javadoc) * * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[]) */ public void sort(int[] data) { int temp; for (int i = 0; i data.length; i++) { int lowIndex = i; for (int j = data.length - 1; j > i; j--) { if (data[j] < data[lowIndex]) { lowIndex = j; } } SortUtil.swap(data,i,lowIndex); } } }
相似回答
大家正在搜