列表转置

List transposition

我想做的是转换我已有的列表。确切地说,从列表到 table。

在收到保存在数组{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;
    }
}

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

您需要遍历结果集,然后打印总金额。 我想你有不同的描述,应该计算总数。 您可以使用不允许重复键作为描述和值作为总金额的 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> 
    }