首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法从Enumeration<TreeNode>转换为Enumeration<DefaultMutableTreeNode>

无法从Enumeration<TreeNode>转换为Enumeration<DefaultMutableTreeNode>
EN

Stack Overflow用户
提问于 2021-10-08 13:47:15
回答 1查看 81关注 0票数 1

在执行任务时,我遇到了"Legacy code",它是用Java8编译的,但不是用Java16编译的,它使用的是这段特定的代码。

代码语言:javascript
复制
protected void onCalculate() {
    final ConsoleMapperForTree cm = new ConsoleMapperForTree();
    cm.setVisible(true);
    cm.getConsole().setFont(new Font("Tahoma", 0, 20));
    final DefaultMutableTreeNode root = (DefaultMutableTreeNode)this.tree.getModel().getRoot();
    final Enumeration<DefaultMutableTreeNode> enmFclt = (Enumeration<DefaultMutableTreeNode>)root.children(); // this is highlighted as an error
    while (enmFclt.hasMoreElements()) {
        final DefaultMutableTreeNode fclt = enmFclt.nextElement();
        int nSpec = 0;
        final Enumeration<DefaultMutableTreeNode> enmDep = (Enumeration<DefaultMutableTreeNode>)fclt.children(); // this is highlighted as an error
        while (enmDep.hasMoreElements()) {
            final DefaultMutableTreeNode dep = enmDep.nextElement();
            final Object data = dep.getUserObject();
            if (((Dept)data).spec) {
                ++nSpec;
            }
        }
        System.out.println(fclt + " has " + nSpec + " specdepartments");
    }
}
EN

回答 1

Stack Overflow用户

发布于 2021-11-09 22:14:22

问题是Java希望您对树的元素执行类型转换,而不是对整个树执行类型转换。因此,请尝试按如下方式移动类型转换运算符:

代码语言:javascript
复制
protected void onCalculate() {
    final ConsoleMapperForTree cm = new ConsoleMapperForTree();
    cm.setVisible(true);
    cm.getConsole().setFont(new Font("Tahoma", 0, 20));
    final DefaultMutableTreeNode root = (DefaultMutableTreeNode)this.tree.getModel().getRoot();
    final Enumeration<TreeNode> enmFclt = root.children(); // this is highlighted as an error
    while (enmFclt.hasMoreElements()) {
        final DefaultMutableTreeNode fclt = (DefaultMutableTreeNode) enmFclt.nextElement();
        int nSpec = 0;
        final Enumeration<TreeNode> enmDep = fclt.children(); // this is highlighted as an error
        while (enmDep.hasMoreElements()) {
            final DefaultMutableTreeNode dep = (DefaultMutableTreeNode) enmDep.nextElement();
            final Object data = dep.getUserObject();
            if (((Dept)data).spec) {
                ++nSpec;
            }
        }
        System.out.println(fclt + " has " + nSpec + " specdepartments");
    }
}

这种并列比较显示了不同之处(单击可放大):

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69496907

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档