【问题标题】:Content of arraylist overwriting by another elementarraylist 的内容被另一个元素覆盖
【发布时间】:2013-06-06 22:42:24
【问题描述】:

我在课堂上有以下代码。 当它处理时,它会生成具有相同标签值的 xml,该标签值位于数据库的最新行中。 我什至尝试重新初始化对象,但它不起作用

while (tempResultSet.next()) {
        conList = new ContentList();    
        conChannel = new ContentChannel();
        conChannel.setType(String.valueOf(tempResultSet.getInt("Key")));

        pubDate.setStart(tempResultSet.getTimestamp("PUBLISHSTARTDATETIME").toString());



       conElement.setPubDate(pubDate);
        conElement.setConChannel(conChannel);



        conList.setConElement(conElement);
        newConList.add(conList);

        conList = null;
        conChannel = null;
        }

【问题讨论】:

  • 您在哪里打印(查看)XML 文件? ContentList(); 是什么?

标签: java arraylist jaxb resultset


【解决方案1】:

您还需要一个新的conElement。它在循环中被重用/覆盖。

目前,您的所有 conList 对象都具有conElement 对象的相同 副本,该副本仅保留通过设置器为最后一行设置的最后一个值ResultSet。做类似的事情

ContentElement conElement = new ContentElement();

conElement.setPubDate(pubDate); // won't overwrite dates
conElement.setConChannel(conChannel); // and channels now

conList.setConElement(conElement); // every list has its own copy of element

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-29
    • 2014-03-30
    • 2015-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-11
    相关资源
    最近更新 更多