我有List
的Nodes
其中包含嵌套列表child nodes
。我试图遍历所有这些节点以找到特定的节点。目前,我从root
级别开始child nodes
,然后使用for-each
循环进行一级深度到sub child node
等等。 这是我的代码:迭代期间交换集合
List<Node> children = root.getChildren();
boolean found = false;
while (!found) {
for (Node node : children) {
if (!node.getData().toString().toUpperCase().contains("BRANCH")) {
if(condition){//some processing}
} else {
//swap children with sub children
if (children.get(0) != null) {
children = children.get(0).getChildren(); // this operation is not possible during iteration
}
}
} else {
continue;
}
}
}
}
如果child node
没有找到任何匹配,那么我需要sub child node
交换收集和不断迭代等。 有没有更好的方法来遍历儿童的嵌套nodelist
?
这似乎是一个XY问题。我会建议递归,但不清楚你实际正在做什么。 –
@JornVernee:我试图从arraylist构建java层次结构树,然后转换为json(gson)在网页中呈现。 –