如何使外部for循环在java中用于内部循环

时间:2016-02-28 14:06:39

标签: java

我有一个名为经纪人发票的下面的类,如下所示 broker invoice class有一个成员变量,其返回类型是数组

public class BrokerInvoice
{

private BrokerInvoiceLineItem[] lineItemsView;

}


public class BrokerInvoiceLineItem
{
}

现在下面是我操作的部分,如图所示,我正在阅读单个选项卡excel表并填充brokerInvoice对象中的所有内容,然后再获取BrokerInvoiceLineItem的值

 brokerInvoice = readinginvoiceimpl.findHeaderRowNumber(workbookXls, 0, brokerInvoiceLineItemList, brokerInvoice , brokerIdLong , finalfilteredfilename,dateType );


for (BrokerInvoiceLineItem item : brokerInvoice
                                .getLineItems()) {

                            if (item.getreaddeate() == null) {

                                throw new BOARuntimeException(
                                        "readdeate is not there" );
                            }

现在问题出现了,如果我正在阅读多个标签delimeted excel表,其中每个标签数据与brokerInvoice对象相关联,所以最后我将它们添加到名为totalbrokerinvoiceobjects的单个列表中

  //Reading multitabs sheets from excel workbook
            for (int i = 0; i < workbookXls.getNumberOfSheets(); i++) {
            List<BrokerInvoiceLineItem> brokerInvoiceLineItemList = new ArrayList<BrokerInvoiceLineItem>();
            brokerInvoice = readinginvoiceimpl.findHeaderRowNumber(workbookXls, 0, brokerInvoiceLineItemList, brokerInvoice , brokerIdLong , finalfilteredfilename,dateType );
            totalBrokerInvoiceObjects.add(brokerInvoice);
            }

现在所有代理发票对象都在名为totalBrokerInvoiceObjects的列表中,请告知我如何执行每个brokerInvoice对象的检查,因为我现在必须检查名为totalBrokerInvoiceObjects <列表中的所有代理发票对象brokerInvoice.getLineItems()) / p>

我应该首先启动外部for循环,首先获取每个代理发票对象,然后在该brokerInvoice对象上检查获取行项目,就像我之前用来做的那样

1 个答案:

答案 0 :(得分:2)

我认为您需要在List循环之外移动outer实例化,并使用addAll将所有内容合并到一个列表中:

List<BrokerInvoiceLineItem> brokerInvoiceLineItemList = new ArrayList<BrokerInvoiceLineItem>();
for (int i = 0; i < workbookXls.getNumberOfSheets(); i++) {
    brokerInvoice = readinginvoiceimpl.findHeaderRowNumber(workbookXls, 0, brokerInvoiceLineItemList, brokerInvoice , brokerIdLong , finalfilteredfilename,dateType );
    for (BrokerInvoiceLineItem item : brokerInvoice.getLineItems()) {
        if (item.getreaddeate() == null) {
            throw new BOARuntimeException("readdeate is not there" );
        }
    }
    totalBrokerInvoiceObjects.addAll(brokerInvoice.getLineItems());
}