5

我正在使用private TreeNode root;创建动态树。

我用

<p:tree value="#{bean.root}" var="node">
    <p:treeNode>
        h:outputText value="#{node}" />
    </p:treeNode>
</p:tree>

在我的页面中显示它。

我的问题是如何删除空节点(不包含子节点)

例子:

node1
   child 1
   child 2
node2 
node3
  child 1

(节点2为空,如何删除?)


使用扩展精度的外部库时,还要检查它们是否使用旧式d1mach.f、r1mach.f、i1mach.f来获取有关机器算术的信息。这里可能需要调整一些值。

Lapack 不会有问题,它使用 dlamch.f(此处为http://www.netlib.org/lapack/util/dlamch.f),它使用 Fortran 90 内在函数来获取这些机器常量。

但是,如果您使用 BLAS 或 SLATEC,它可能会出现问题。

4

2 回答 2

6

您可以首先让所有孩子循环树:

List<TreeNode> nodes = this.root.getChildren();

然后你也许可以做这样的事情:

List<TreeNode> nodes = ....
Iterator<TreeNode> i = nodes.iterator();
while (i.hasNext()) {
   TreeNode = i.next(); 
   // Use isLeaf() method to check doesn't have childs.
   i.remove();
}

这将是下一个代码的正确版本,因为我猜您无法在循环中删除集合元素。

for (TreeNode treeNode : nodes) {
   if(treeNode.isLeaf()){
       TreeNode parent = treeNode.getParent();
       parent.getChildren().remove(treeNode);
   }
}

希望能帮助到你。

问候。

于 2012-10-23T08:10:25.197 回答
0

我用了这段代码

dropNode.getParent().getChildren().removeIf(n -> n==dropNode);
于 2021-11-18T22:10:27.383 回答