You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

列表转置技术实现咨询:将列表转换为表格及数组赋值逻辑

转置月度金额数组为表格的实现方案

Alright, let's break down how to turn that monthly amount array of yours into a clean table, based on the code logic you laid out. First, let's recap your current setup: you're using a 13-length array (index 0 is unused, 1-12 map to January-December) to store amounts fetched from your database, plus a running total of all monthly amounts. Transposing this into a table just means mapping each array index (month) to its corresponding value (amount) in table rows, then adding a total row at the end.

First, let's formalize your core existing code

// Initialize 13-length array (index 0 unused, default all 0s)
double[] values = new double[13];
double monthTotal = 0.0;

// Populate array from ResultSet
while (rsSol.next()) {
    int month = rsSol.getInt("month"); // Get month number (1-12)
    double mon_amounth = rsSol.getDouble("amounth"); // Get amount for the month
    values[month] = mon_amounth; // Store amount in the correct array position
    monthTotal += mon_amounth; // Accumulate total amount
}

Option 1: Print a table to the console

If you just need a quick table for debugging or console output, use this loop to build and print rows:

// Print table header
System.out.println("| Month | Amount |");
System.out.println("|-------|--------|");

// Loop through months 1-12 to create table rows
for (int i = 1; i <= 12; i++) {
    System.out.printf("| %d     | %.2f  |%n", i, values[i]);
}

// Print total row
System.out.println("|-------|--------|");
System.out.printf("| Total | %.2f  |%n", monthTotal);

Option 2: Generate an HTML table (for web display)

If you need to render this table on a webpage, build an HTML string like this:

StringBuilder tableBuilder = new StringBuilder();
tableBuilder.append("<table border='1' cellpadding='8'>");
// Add header row
tableBuilder.append("<tr><th>Month</th><th>Amount</th></tr>");

// Add monthly rows
for (int i = 1; i <= 12; i++) {
    tableBuilder.append(String.format("<tr><td>%d</td><td>%.2f</td></tr>", i, values[i]));
}

// Add bold total row
tableBuilder.append(String.format("<tr><td><strong>Total</strong></td><td><strong>%.2f</strong></td></tr>", monthTotal));
tableBuilder.append("</table>");

// Use the tableBuilder.toString() wherever you need the HTML
System.out.println(tableBuilder.toString());

Quick tips to refine this

  • Skip unused index: Always loop from i=1 to i=12—don't include index 0, since it's just an unused default 0.0 value.
  • Format amounts: Using %.2f ensures amounts show with two decimal places, which is standard for currency. Adjust the number (like %.0f for whole numbers) if needed.
  • Handle missing data: If some months have no data in your database, the array will keep the default 0.0. To show a "No Data" message instead, add a quick check:
    String amountDisplay = values[i] == 0.0 ? "No Data" : String.format("%.2f", values[i]);
    // Use amountDisplay in your table row instead of values[i]
    

内容的提问来源于stack exchange,提问作者WiLLyxVKei

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:48:26