java - 列表转置

标签 java jsp web code-snippets

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

problem description

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

关于java - 列表转置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50158202/

相关文章:

angularjs - Web 应用 list : hash in start_url

java - 从 JVM 访问 token 端点时出现 Google 身份验证错误

java - 如何构建只有一处不同的 3 个类

java - 单击 ListItem 后如何更改 fragment 布局?

java - 从多个点创建平滑曲线并按百分比或时间获取该曲线上的位置

java - 从 eclipse 导出 WAR,找不到 mysql 驱动程序。从 Eclipse 运行时很好

javascript - 专用提交按钮使用相同的 servlet 将表单数据单独保存到数据库

java - 使用 Eclipse 和 JBoss 调试 JSP

javascript - 如何做 HTML 谷歌多级条目?

java - 如何使用 Java 连接到 WSDL 服务?