2011-10-06 52 views
1

考虑这段代码使用Java规则集正确

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

当我做了ruleSet.clear()我也失去了我以前在parentChildrenList2.get(cursor).setChildRules(ruleSet);

设置的值如何才能停止失去它,但在同一时间结算ruleSet

回答

0

请注意setChildRules()(可能)不会创建引用的Set的副本。它只是复制(“记住”)对该Set的引用。如果您以后修改Set(如致电clear()),那么这将是可见的大家谁参照Set

似乎像你想在parentChildrenList2每个元素都有自己的Set。因此,您实际上需要用ruleSet = new HashSet()(或您使用的任何类型)替换ruleSet.clear()

+0

非常感谢。有用。一旦我能,将会标记为正确的答案。 – Sergiu