0

使用DefaultTreeModel, defaultmutbletreenode'sgetRoot()返回它的最高祖先,但是你将如何向下一代返回它的第二高祖先?

root
 - ancestor 1
  - some parent
   - some child
 - ancestor 2
  - some parent
   - another parent
    - some child

那么如何找到ancestor 1some child在这个分支中给出,每个分支的深度对于ancestor下的每个节点都不同root

我需要遍历ancestor 1from some child,并且还需要一个更深的分支,给定some child,它会找到ancestor 2.

4

1 回答 1

0

尝试这个:

TreeNode[] nodeArray = tree.getPathToRoot(nodeInQuestion);
TreeNode secondFromRoot;

if ((nodeArray != null) && // I'm not sure this can actually happen.
    (nodeArray.length > 1)) // current node is not the root node.
{
   secondFromRoot = nodeArray[1];
}
else
{
   ... decide what makes sense here.
}
于 2012-03-06T18:51:10.667 回答