簡體   English   中英

Java:如何實現類似於Eclipse軟件包瀏覽器Tree的Tree結構

[英]Java: How to implement Tree structure similar to eclipse package explorer 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("  ");
        }
    }
}

詢問我是否對代碼有任何疑問。

編輯:當然,這僅在方法列表按字母順序排序時才有效。 因此,如果不是這種情況,則將是第一步。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM