List
のNodes
には、ネストされたリストchild nodes
が含まれています。私は特定のノードを見つけるためにそれらのすべてを反復しようとしています。現在root
レベルからchild nodes
で始まり、次にsub child node
に1レベル深く進み、for-each
ループを使用します。 これは私のコードです:繰り返し中のコレクションの交換
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)に変換してWebページにレンダリングしようとしていました。 –