如何在迭代列表时返回所有值?

时间:2013-05-30 21:02:02

标签: java arraylist iterator

public Details getDetails(String id)throws InvokerException {     详情详情=新详情();

try {

    URL url = new URL(BaseUrl, "/cxf/query/ask?id=" + id);
    LOGGER.trace("URL: {}", url);

    String xml = queryStore(url);
    LOGGER.trace("Query result: {}", xml);

    details = new Details();
    InputSrc source = new InputSrc(new StringReader(xml));
        ResultsContentHandler handler = new ResultsContentHandler();

    XMLReader reader = XMLReaderFactory.createXMLReader();
    reader.setContentHandler(handler);
    reader.parse(source);

    for (Hashtable<String,String> result : handler.getResultSet()) {
        String baseId = result.get("baseId");
        ArrayList<Details> list = getHistoryDetails(baseId );

        for(Details t : list) {
        details.setStatus(t.getStatus());
        }
    }
} catch (Exception e) {
    throw new InvokerException(e);
}
return details;

}

我想要实现的是返回Arraylist中所有项目的详细信息。例如,我期望不止一种状态,但目前我只获得一种状态。

详细信息类

public class Details {
    private Core core;
    private String department;
    private GregorianCalendar timestampReceived;
    private GregorianCalendar timestampReported;
    private String status;
    private GregorianCalendar timestampStatus;
    private String explanation;

    public Details(){}

    public Details(Core core,
            String department, GregorianCalendar timestampReceived,
            GregorianCalendar timestampReported, String status,
            GregorianCalendar timestampStatus, String explanation) {
        super();
        this.core = core;
        this.department = department;
        this.timestampReceived = timestampReceived;
        this.timestampReported = timestampReported;
        this.status = status;
        this.timestampStatus = timestampStatus;
        this.explanation = explanation;
    }
    public Core getCore() {
        return core;
    }

    public String getDepartment() {
        return department;
    }
    public GregorianCalendar getTimestampReceived() {
        return timestampReceived;
    }

    public GregorianCalendar getTimestampReported() {
        return timestampReported;
    }

    public String getStatus() {
        return status;
    }

    public GregorianCalendar getTimestampStatus() {
        return timestampStatus;
    }

    public String getExplanation() {
        return explanation;
    }

    public void setCore(Core core) {
        this.core = core;
    }

    public void setDepartment(String department) {
        this.department = department;
    }

    public void setTimestampReceived(GregorianCalendar timestampReceived) {
        this.timestampReceived = timestampReceived;
    }

    public void setTimestampReported(GregorianCalendar timestampReported) {
        this.timestampReported = timestampReported;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public void setTimestampStatus(GregorianCalendar timestampStatus) {
        this.timestampStatus = timestampStatus;
    }

    public void setExplanation(String explanation) {
        this.explanation = explanation;
    }


}

4 个答案:

答案 0 :(得分:1)

只需对一个方法调用就不能返回多个值,但是你应该做的是返回一个ArrayList:

public List<Details> getDetails(String id) throws InvokerException {
    List<Details> details = new ArrayList<Details>();

    try {

        URL url = new URL(BaseUrl, "/cxf/query/ask?id=" + id);
        LOGGER.trace("URL: {}", url);

        String xml = queryStore(url);
        LOGGER.trace("Query result: {}", xml);

        InputSrc source = new InputSrc(new StringReader(xml));
            ResultsContentHandler handler = new ResultsContentHandler();

        XMLReader reader = XMLReaderFactory.createXMLReader();
        reader.setContentHandler(handler);
        reader.parse(source);

        Details detail;
        for (Hashtable<String,String> result : handler.getResultSet()) {
            String baseId = result.get("baseId");
            ArrayList<Details> list = getHistoryDetails(baseId );

            for(Details t : list) {
            detail = new Details();
            detail.setStatus(t.getStatus());
            details.add(detail);
            }
        }
    } catch (Exception e) {
        throw new InvokerException(e);
    }
    return details;
}

答案 1 :(得分:0)

您需要返回List<Details>,而不只是返回详细信息。因此,您希望在代码示例中返回变量“list”。

答案 2 :(得分:0)

创建ArrayList<Details>()并向其添加详细信息。然后,返回此列表。

代码:

List<Details> detailsToReturn = new ArrayList<>();

for (Hashtable<String,String> result : handler.getResultSet()) {
            String baseId = result.get("baseId");
            ArrayList<Details> list = getHistoryDetails(baseId );

            for(Details t : list) {
                Details temp = new Details();
                temp.setStatus(t.getStatus();
                detailsToReturn.add(temp);
            }
        }

return detailsToReturn;

您提供的代码非常不清楚,如果这不是您想要的,我建议您为我们清除它。

答案 3 :(得分:0)

您只创建一个 Details对象(实际上是两个,但第一个被扔掉)。如果要保留从Details检索的每个ArrayList对象,则每次迭代for循环时都需要创建一个新的Details对象。您还应将这些添加到新的List

当然,只需将从ArrayList获得的每个getHistoryDetails()附加到新列表中,即可为自己节省大量工作。