0

我有一个 bean 列表,我正在检查该列表中是否至少有 2 个带有返回“VALUEA”和“VALUEB”的 getter 的 bean。这样我就可以为他们两个构建一个名为“VALUEC TOTAL”的“总”bean。我还在最后做了一个“总计”的豆排。

我的问题只是“VALUEC TOTAL”。我需要在原始列表中的“VALUEB”之后将该 bean 添加到列表中,而不是我在最后的操作方式。

我可以确认摘要不为空。

summary = new ArrayList<WorkoutPlanBean>();
beans = dao.getAppropriationSummaryBySor(criteria, summary);

//Here we are setting up lists to be used to be added to the bottom of "Rollup" as "Totals" 
List<WorkoutPlanBean> valuecList = new ArrayList<WorkoutPlanBean>();
List<WorkoutPlanBean> sumList = new ArrayList<WorkoutPlanBean>();
for (WorkoutPlanBean b: summary)
{
    //We need 2 beans...one with VALUEA and one with VALUEB
    if( b.getSor().getShortName().equals("VALUEA") || b.getSor().getShortName().equals("VALUEB"))
    {
        valuecList.add(b);
    }
    sumList.add(b);
}
// Create VALUEC totals bean.
if (valuecList.size() > 1 )
{
    WorkoutPlanBean valuecTotals = new WorkoutPlanBean();
    valuecTotals.setSor(VALUEC_TOTALS_SOR);

    for (WorkoutPlanBean bean : valuecList)
    {
        valuecTotals.setQuantity(Overview.totalsIntegerAdd(valuecTotals.getQuantity(),
                                 bean.getQuantity()));

        valuecTotals.setHours(Overview.totalsDoubleAdd(valuecTotals.getHours(),
                              bean.getHours()));

        valuecTotals.setValue(Overview.totalsBigDecimalAdd(valuecTotals.getValue(),
                              bean.getValue()));
     }
     summary.add(valuecTotals); 
 }

 // Create Totals bean.
 if (sumList.size() > 1 )
 {
     WorkoutPlanBean globalTotals = new WorkoutPlanBean();
     globalTotals.setSor(GRAND_TOTALS_SOR);

     for (WorkoutPlanBean bean : sumList)
     {
         globalTotals.setQuantity(Overview.totalsIntegerAdd(globalTotals.getQuantity(),
                                  bean.getQuantity()));
         globalTotals.setHours(Overview.totalsDoubleAdd(globalTotals.getHours(),
                               bean.getHours()));
         globalTotals.setValue(Overview.totalsBigDecimalAdd(globalTotals.getValue(),
                               bean.getValue()));
     }
     summary.add(globalTotals); 
 }

 request.setAttribute("summary", summary);
4

2 回答 2

2

您需要获取所需的索引并使用: list.add(index, yourBean)

于 2013-02-25T15:39:28.110 回答
1

您可以使用索引而不是 for-each 循环在列表中进行迭代:

for(int i = 0; i < summary.size(); i++){
    WorkoutPlanBean b = summary.get(i);
    ...
}

所以你可以记住你最后一个VALUEA/VALUEB的索引,然后你可以summary.add(index, element)你的总和......

无论如何看看ArrayList Javadoc

顺便说一句,您还可以在第一个循环期间计算两个总数,而不需要 valuecList 和 sumList ...

这是一个仅编辑代码的示例,因此您可能必须更正一些拼写错误:

summary = new ArrayList<WorkoutPlanBean>();
    beans = dao.getAppropriationSummaryBySor(criteria, summary);

    WorkoutPlanBean valuecTotals = new WorkoutPlanBean();
    valuecTotals.setSor(VALUEC_TOTALS_SOR);

    WorkoutPlanBean globalTotals = new WorkoutPlanBean();
    globalTotals.setSor(GRAND_TOTALS_SOR);

    int nbMdmc = 0; // to count VALUEA & VALUEB to know if a total is needed
    int lastMdmcIndex = 0;

    for (int i=0; i< summary.size(); i++)
    {
        WorkoutPlanBean bean = summary.get(i);

        //We need 2 beans...one with VALUEA and one with VALUEB
        if( bean.getSor().getShortName().equals("VALUEA") || bean.getSor().getShortName().equals("VALUEB"))
        {
            lastMdmcIndex = i;
            // count VALUEA and VALUEB
            nbMdmc++;

            // add to valuec total
            valuecTotals.setQuantity(Overview.totalsIntegerAdd(valuecTotals.getQuantity(),
                                                               bean.getQuantity()));
            valuecTotals.setHours(Overview.totalsDoubleAdd(valuecTotals.getHours(),
                                                                bean.getHours()));
            valuecTotals.setValue(Overview.totalsBigDecimalAdd(valuecTotals.getValue(),
                                                                bean.getValue()));
        }

        // add to grand total
        globalTotals.setQuantity(Overview.totalsIntegerAdd(globalTotals.getQuantity(),
                                                               bean.getQuantity()));
        globalTotals.setHours(Overview.totalsDoubleAdd(globalTotals.getHours(),
                                                                bean.getHours()));
        globalTotals.setValue(Overview.totalsBigDecimalAdd(globalTotals.getValue(),
                                                                bean.getValue()));

    }

    if(nbMdmc > 1) summary.add(lastMdmcIndex+1, valuecTotals); 

    if(summary.size() > 1) summary.add(globalTotals); 


request.setAttribute("summary", summary);
于 2013-02-25T15:48:05.857 回答