正确使用Java规则集

正确使用Java规则集,java,for-loop,Java,For Loop,考虑到这段代码 for (MyRule cr : crList) { if (crIds.contains(cr.getParentId())) { ruleSet.add(cr); for (int cursor = 0; cursor < parentChildrenList.size(); cursor++) { if (parentChildrenList.get(cursor).getId().equals(cr.getParentId(

考虑到这段代码

for (MyRule cr : crList) {    
  if (crIds.contains(cr.getParentId())) {
    ruleSet.add(cr);

    for (int cursor = 0; cursor < parentChildrenList.size(); cursor++) {
      if (parentChildrenList.get(cursor).getId().equals(cr.getParentId())) {
        parentChildrenList2.get(cursor).setChildRules(ruleSet);
        parentChildrenList2.remove(cursor + 1);
      }
    }
  }
  ruleSet.clear();
}
for(MyRule cr:crList){
if(crIds.contains(cr.getParentId())){
规则集。添加(cr);
对于(int cursor=0;cursor
执行
ruleSet.clear()
时,我还丢失了以前在
parentChildrenList2.get(游标).setChildRules(规则集)中设置的值


如何停止丢失它,但同时清除
规则集

请注意
setChildRules()
(可能)不会创建
规则集
引用的
的副本。它只是复制(“记住”)对该
集的引用。如果以后修改该
(例如调用
clear()
),那么对该
有引用的每个人都可以看到


似乎您希望
parentChildrenList2
中的每个元素都有自己的
。因此,您实际上需要将
ruleSet.clear()
替换为
ruleSet=new HashSet()
(或您使用的任何类型)。

非常感谢。它起作用了。一旦我能,我会记为正确答案。