首页 > 解决方案 > 列表转置

问题描述

我想做的是对我拥有的列表进行换位。准确地说是从一个列表变成一个表格。

问题描述

在我收到一个保存在数组{13}中的值的同时(这个数组用 0 初始化。每个月一个,第一个 0 不使用)

month           =  rsSol.getInt("month");      // To save number of month
mon_amounth     =  rsSol.getDouble("amounth"); // Save amount in a value
values[month]   =  mon_amounth;                // Save amount in the array
monthTotal     +=  giro_montoMes;              // Sum of each mon_amounth

然后,这是我卡住的一段代码。

我对需要添加到组描述并在同一行中打印所有金额的条件有点困惑。

while ( rsSol.next() ) {
    //where i (re)write description
    if ( desc.equals(empty_string) ){
        desc        =  rsSol.getString("description");
    }

    if ( !desc.equals(rsSol.getString("giro")) && count > 1 ){
        desc        = rsSol.getString("giro");
    } 

    // Where i print the description
    <tr class>
        <td border='0' ALIGN='left'>    desc.toLowerCase()  </td>

        // Where i print the array
        for(int i=1; i<=12; i++){
            <td border='0' ALIGN='right'>   values[i])  </td>
        }

        // Where i print the sum of all months
        <td border='0' ALIGN='right'>   monthTotal  </td>
    </tr>

    // Setting values to 0 and cleaning the array
    mon_amounth =   0;

    for ( int i=1 ; i<=12 ; i++ ) {
        values[i] = 0;
    }
}

任何帮助都会非常有帮助。提前致谢。

标签: javajspwebcode-snippets

解决方案


您需要遍历您的结果集,然后打印您的总金额。我想你有不同的描述,应该总共计算。您可以使用不允许重复键作为描述和值作为您的总量的 HashMap。

// collect sums
    Map<String, Integer[]> totals = new HashMap<>();
    while ( rsSol.next() ) {
        if ( desc.equals("") ){
            desc =  rsSol.getString("description");
        }
        if ( !desc.equals(rsSol.getString("giro")) && count > 1 ){
            desc = rsSol.getString("giro");
        }
        totals.putIfAbsent(desc, new Integer[12]);
        Integer[] totalSums = totals.get(desc);
        for(int i = 0; i < 12; i++){
            totalSums += values[i];
        }
    }

// print your results
    for (String desc : totals.keySet()) {
       <tr class>
            <td border='0' ALIGN='left'>    desc.toLowerCase()  </td>

                // Where i print the array
                for(int i=1; i<=12; i++){
                <td border='0' ALIGN='right'>   totals.get(desc)  </td>
                }

                // Where i print the sum of all months
            <td border='0' ALIGN='right'>   monthTotal  </td>
        </tr> 
    }

推荐阅读