Java:如何实现类似于eclipse package explorer Tree的Tree结构

标签 java algorithm data-structures tree

我有一个存储为字符串表示的方法列表 “com.company.project.service.service1Impl.method()” “com.company.project.service.service2Impl.method()”

....

具有完整的类/包签名

实现树结构以类似于 eclipse 包资源管理器的方式显示包/类/方法的最合适方法是什么?

例如:

com
  mycompany
    myproject1
       service
         service1Impl
             method1
             method2
         service2impl
       controller
          controllerImpl
             method1
             method2
          controllerImpl2
    myproject2

注意:

不确定这是否会有所不同,但我计划将此数据结构转换为 json 以在 UI 中的 jquery 树中显示它。

提前致谢。

最佳答案

我会用具有以下参数的递归方法解决它:

  • 包含字符串的数组
  • 当前前缀
  • 当前深度
  • 最大深度(所以只需要计算一次)

我认为最好的解释方式是使用实际代码:

import java.util.ArrayList;

public class Test {

    public static void main(String[] args) {
        Test t = new Test();
        String s1 = "com.company.project.service.service1Impl.method()";
        String s2 = "com.company.project.service.service2Impl.method()";
        String s3 = "com.company.test.service.service1Impl.method()";
        String s4 = "com.company.test.service.service2Impl.method()";
        String[] strings = { s1, s2, s3, s4 };
        t.print(strings);
    }

    public void print(String[] strings) {
        //calculate max depth
        int maxDepth = 0;
        for (String string : strings) {
            int currentDepth = string.split("\\.").length;
            if (currentDepth > maxDepth) {
                maxDepth = currentDepth;
            }
        }
        this.print(strings, "", 0, maxDepth);
    }

    public void print(String[] strings, String start, int currentDepth,
            int maxDepth) {
        if (currentDepth == maxDepth - 1) {
            return;
        }
        String currentPrint = null;
        ArrayList<String> candidates = new ArrayList<String>();

        // add candidates
        for (String s : strings) {
            if (!s.startsWith(start)) {
                continue;
            }
            String[] split = s.split("\\.");
            if (split.length - 1 < currentDepth) {
                continue;
            }
            if (currentPrint == null) {
                currentPrint = split[currentDepth];
                candidates.add(currentPrint);
                continue;
            }
            if (!currentPrint.equals(split[currentDepth])) {
                currentPrint = split[currentDepth];
                candidates.add(currentPrint);
            }
        }

        // print depth+1 with candidates
        currentDepth++;
        for (String c : candidates) {
            // print current level
            this.printSpaces(currentDepth - 1);
            System.out.println(c);
            // we have to go deeper
            this.print(strings, start + c + ".", currentDepth, maxDepth);
        }
    }

    // print spaces
    public void printSpaces(int max) {
        for (int i = 0; i < max; i++) {
            System.out.print("  ");
        }
    }
}

如果您对代码有任何疑问,请问我。

编辑:这当然只有在方法列表按字母顺序排序时才有效。因此,如果不是这种情况,则排序将是第一步。

关于Java:如何实现类似于eclipse package explorer Tree的Tree结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16762124/

相关文章:

java - 用于大量数据输入的斐波那契数列的第 n 项(无递归或循环)

algorithm - 从另一组中找到一组中的最远点

algorithm - 我可以使用哪些数据结构来满足下面提到的需求?

java - 更好的数据结构,用于检索日期之间的数据

java - Spring AMQP 集成 - 消费者手册确认

java - 如何确保 java 中的注释执行顺序?

arrays - 计算相似度数大于 K 的子数组

c# - 如何按以下顺序显示月份名称

java - 如何获取 Xpath 表达式的子树?

java - 在数组索引处查找字符串? java