【问题标题】:weird java Iterator items being replaced奇怪的java迭代器项目被替换
【发布时间】:2014-01-14 16:51:29
【问题描述】:

所以,在我的代码中,我得到一个组件列表,迭代组件,在每个组件内部,我得到组件的维护并迭代它们。问题是,这些项目正在被替换并且没有像我希望的那样重新创建:

List<ComponentRelation> componentRelations = getComponentRelations();

List<MaintenanceRequired> allMaintenancesRequired = new ArrayList<MaintenanceRequired>();

for (Iterator<ComponentRelation> iterator = componentRelations.iterator(); iterator.hasNext();) {
    ComponentRelation componentRelation = iterator.next();
    System.out.println("Getting maintenances from "+componentRelation.getComponent().getName()+" at position "+ComponentInstallationPosition.getPositionString(componentRelation.getInstallationPosition()));
    for (Iterator<MaintenanceRequired> iterator2 = maintenancesDAO.getRequiredMaintenancesByComponentRelation(componentRelation).iterator(); iterator2.hasNext();) {
        MaintenanceRequired maintenanceRequired = (MaintenanceRequired) iterator2.next();
        maintenanceRequired.setMaintenancePerformedOnComponent(omponentRelation);
            allMaintenancesRequired.add(maintenanceRequired);
    }
}

所以,println 是正确的,但在项目内部:

public void setMaintenancePerformedOnComponent(ComponentRelation maintenancePerformedOnComponent) {
    if (this.maintenancePerformedOnComponent!=null)
        System.out.println("SETTING "+ComponentInstallationPosition.getPositionString(this.maintenancePerformedOnComponent.getInstallationPosition())+" to "+ComponentInstallationPosition.getPositionString(maintenancePerformedOnComponent.getInstallationPosition()));
    else System.out.println("SETTING "+ComponentInstallationPosition.getPositionString(maintenancePerformedOnComponent.getInstallationPosition()));
    this.maintenancePerformedOnComponent = maintenancePerformedOnComponent;
}

所以印刷品说:

"getting maintenances from X-1, position 1"
"SETTING 1"
"SETTING 1"
"SETTING 1"
"SETTING 1"
"SETTING 1"
"getting maintenances from X-2, position 2"
"SETTING 1 to 2"
"SETTING 1 to 2"
"SETTING 1 to 2"
"SETTING 1 to 2"
"SETTING 1 to 2"

那么,如果是另一个对象,他为什么要将 1 设置为 2??

按照约翰的要求,我做了什么:

create empty list of items Y;

get list of items X;

iterator of list of items X {
    get from database all Y inside X and iterate{
        set iterated Y that he has a position K;
        put iterated Y inside created initially empty list of Y; // first line
    }
}

【问题讨论】:

  • 静态类变量?
  • 如果您想出一个简短但完整的示例来说明问题,那会更容易帮助您 - 理想情况下没有这么长的示例 -冗长而明确的业务特定名称。
  • 不是静态的.. 现在添加更简单的示例
  • 添加了我在做什么的“简要”,有人知道为什么会这样吗?
  • @VitorMendes 不幸的是,您的“更简单的示例”并没有多大帮助;编辑问题以使用类和变量名称将更容易阅读(就像使用 enhanced for loops 一样)。也就是说,我认为@turbo 是对的;如果this.maintenancePerformedOnComponentstatic,您看到的行为就会发生。

标签: java object iterator


【解决方案1】:

仔细看看:

System.out.println("SETTING "+ComponentInstallationPosition.getPositionString(this.maintenancePerformedOnComponent.getInstallationPosition())+" to "+ComponentInstallationPosition.getPositionString(maintenancePerformedOnComponent.getInstallationPosition()));

您引用的不是本地maintenancePerformedOnComponent,而是this.maintenancePerformedOnComponent

【讨论】:

  • 是的,正如所解释的,它在 Object 内部,即 setter。感谢您的回复,还有其他想法吗?
猜你喜欢
  • 2016-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-23
  • 2013-11-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多