Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在遍历列表时返回所有值?_Java_Arraylist_Iterator - Fatal编程技术网

Java 如何在遍历列表时返回所有值?

Java 如何在遍历列表时返回所有值?,java,arraylist,iterator,Java,Arraylist,Iterator,公共详细信息getDetails(字符串id)引发InvokerException{ 详细信息=新的详细信息() 您需要返回一个列表,而不仅仅是详细信息。因此,您希望在代码示例中返回变量“List”。您不能在一次方法调用中返回多个值,但是您应该返回一个ArrayList: public List<Details> getDetails(String id) throws InvokerException { List<Details> details = new

公共详细信息getDetails(字符串id)引发InvokerException{ 详细信息=新的详细信息()


您需要返回一个
列表,而不仅仅是详细信息。因此,您希望在代码示例中返回变量“List”。

您不能在一次方法调用中返回多个值,但是您应该返回一个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;
}
public List getDetails(String id)抛出invokereexception{
列表详细信息=新建ArrayList();
试一试{
URL URL=新URL(BaseUrl,“/cxf/query/ask?id=“+id”);
trace(“URL:{}”,URL);
字符串xml=queryStore(url);
trace(“查询结果:{}”,xml);
InputSrc source=newinputsrc(newstringreader(xml));
ResultsContentHandler处理程序=新的ResultsContentHandler();
XMLReader=XMLReaderFactory.createXMLReader();
setContentHandler(handler);
reader.parse(源代码);
细节;
for(哈希表结果:handler.getResultSet()){
字符串baseId=result.get(“baseId”);
ArrayList=getHistoryDetails(baseId);
详情(t:列表){
细节=新细节();
detail.setStatus(t.getStatus());
详细信息。添加(详细信息);
}
}
}捕获(例外e){
抛出新的InvokerException(e);
}
退货详情;
}
创建一个
ArrayList()
并向其中添加详细信息。然后,返回此列表

代码:

List detailsToReturn=new ArrayList();
for(哈希表结果:handler.getResultSet()){
字符串baseId=result.get(“baseId”);
ArrayList=getHistoryDetails(baseId);
详情(t:列表){
Details temp=新细节();
温度设置状态(t.getStatus();
detailsToReturn.add(临时);
}
}
返回并返回;
您提供的代码非常不清楚,因此如果这不是您想要的,我建议您为我们清除它。

您只创建一个
详细信息
对象(实际上有两个,但第一个被丢弃)。如果要保留从
数组列表
检索到的每个
详细信息
对象,则需要在每次迭代for循环时创建一个新的
详细信息
对象。还应将其添加到新的
列表


当然,只需添加从
getHistoryDetails()获得的每个
ArrayList
,就可以节省大量工作
到一个新列表。

那么你没有按照你认为的方式填充你的ArrayList。改变你的期望或调查它们。我们不会为你这样做。很多人会说返回一个
列表
,而是移动
细节=新细节();
在循环中,用这个新实例填充您的列表,但看起来您自己还没有尝试过任何东西…这个
详细信息类是什么?它是如何工作的?向我们展示一些与解决问题无关的代码!@code Guru。@luigimendoza当然是。如果详细信息类保留了多个项目的列表,那么操作可能使用不正确。另一方面,如果详细信息只是一项,则OP需要推出自己的解决方案(最有可能使用某种列表)。如果你分析整个代码,你会看到很多错误,你应该重新编辑这个答案,并为所有错误提供解决方案。这可能会让他走上正确的方向。我不明白为什么要创建一个新的
详细信息
实例来设置
状态
属性,然后将其添加到列表中。为什么不直接添加整个
detail
对象从
ArrayList列表
details
?我同意你的观点,但由于不是我的代码,我试图将其更正为他给我们的代码:),可能他只想要状态,而不是对象所能携带的所有信息。状态只是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;
    }


}
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;
}
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;