正确使用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()
(或您使用的任何类型)。非常感谢。它起作用了。一旦我能,我会记为正确答案。