java - 列表转置
问题描述
我想做的是对我拥有的列表进行换位。准确地说是从一个列表变成一个表格。
在我收到一个保存在数组{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>
}
推荐阅读
- td-engine - tdengine 数据库中的多级存储
- python - 如何在 twint 中使用 since 和 until 参数?
- python - 如何使用 django 将来自 chromedriver 的文件正确保存在 docker 容器中?
- django - 为什么 Django 开发服务器没有重新加载?
- reactjs - 动态填充菜单组件 - React Material UI
- wordpress - 更改 Wordpress 网站的域名
- python - 直接运行 rasa 服务器,而不是从源环境?
- kubernetes - 在 1.20.8-gke.900 集群中找不到 GKE MultidimPodAutoscaler
- python - 如何让 Django 将图像保存到静态文件中
- ios - iOS 15 Safari 在键盘可见时检测浮动地址栏