解决方案 »

  1.   

    层级数不太多的话,直接递归。。
    public Category[] getPath() {
    return this._getPath(1);
    }

    private Category[] _getPath(int level) {
    Category[] path;

    if (parent == null) {
    path = new Category[level];
    } else {
    path = this.parent._getPath(level + 1);
    }

    path[path.length - level] = this;
    return path;
    }
      

  2.   

    或者不用递归,用个List先存着也行
    public Category[] getPath() {
    List<Category> list = new LinkedList<>();

    Category category = this;
    while (category != null) {
    list.add(0, category);
    category = category.parent;
    }


    Category[] path = new Category[list.size()];
    int i = 0;
    for (Category item : list) {
    path[i++] = item;
    }

    return path;
    }
      

  3.   

    perfert, 非常感谢,结帖了。
    感谢