在Java中实现具有层次结构的列表

时间:2014-11-16 18:42:51

标签: java hierarchy

我在Java小任务中遇到了编程概念问题。我有一个Employee类,其中包括两个参数:ID和他的经理的ID。我也有班级员工,这是我的员工的容器。现在我想将它们列在树状层次结构中,如下所示:

Dominik Sankowski
|-Radosław Adamus
| |- Łukasz Sturgulewski
| |- Andrzej Albrecht
|-Lidia Jackowska
| |- Michał Paluch
|-Szymon Grabowski
| |- Wojciech Bieniecki
| |- Sebastian Stoliński
| |- Tomasz Węgliński
|-Jacek Kucharski

取决于谁是谁的老板。

我已经尝试过使用TreeMaps,并且经常复发,但是我的头脑真的爆炸了,我只是无法正确行动,我的思想被阻挡了。有什么提示吗?

EDIT1: 到目前为止我的代码:

public class Staff {
    //Integer - manager id, Employee - object
    private ArrayList <Employee> employees;

    public Staff(){

        employees = new ArrayList<Employee>();
        employees.add(new Employee(1, "Wojciech","Bieniecki", 15));
        employees.add(new Employee(2, "Dariusz","Brzeziński", 18));
        employees.add(new Employee(3, "Michał","Paluch", 19));
        employees.add(new Employee(4, "Sebastian","Stoliński",  1));
        employees.add(new Employee(5, "Tomasz","Węgliński", 1));
        employees.add(new Employee(6, "Radosław","Adamus", 16));
        employees.add(new Employee(7, "Andrzej","Albrecht",  11));
        employees.add(new Employee(8, "Robert","Banasiak",  13));
        employees.add(new Employee(9, "Tomasz","Koszmider",  10));
        employees.add(new Employee(10, "Krzysztof","Strzecha", 16));
        employees.add(new Employee(11, "Łukasz","Sturgulewski",  6));
        employees.add(new Employee(12, "Piotr","Urbanek", 17));
        employees.add(new Employee(13, "Radosław","Wajman",  16));
        employees.add(new Employee(14, "Jacek","Wiślicki",  16));
        employees.add(new Employee(15, "Szymon","Grabowski",  16));
        employees.add(new Employee(16, "Dominik","Sankowski",  0));
        employees.add(new Employee(17, "Jacek","Kucharski",  16));
        employees.add(new Employee(18, "Piotr","Ostalczyk",  16));
        employees.add(new Employee(19, "Lidia","Jackowska",  16));
    }

    public Employee convertIdToEmployee (int id)
    {
        for (int i=0; i<employees.size(); i++)
        {
            if(id == employees.get(i).getId())
            {
                return employees.get(i);
            }
        }
        return null;
    }


    public Employee find(String familyName)
    {
        for (int i=0; i<employees.size(); i++)
        {
            if(familyName == employees.get(i).getFamilyName())
            {
                return employees.get(i);
            }
        }
        return null;

    }

    public ArrayList<Employee> showSubordinates(Employee emp)
    {
        ArrayList <Employee> result = new ArrayList <Employee>();
        for (int i=0; i<employees.size(); i++)
        {
            if(emp.getId() == employees.get(i).getManagerId())
            {
                result.add(employees.get(i));
            }
        }
        return result.size() != 0 ? result : null;
    }

    public ArrayList<Employee> buildTree(ArrayList <Employee> subordinates){

        ArrayList <Employee> result = new ArrayList <Employee>();
        for(int i=0; i<subordinates.size();i++)
        {
            buildTree(showSubordinates(subordinates.get(i)));
            result.add(subordinates.get(i));

        }
        return result;
    }

    public ArrayList<Employee> getEmployees() {
        return employees;
    }

    public void setEmployees(ArrayList<Employee> employees) {
        this.employees = employees;
    }

    @Override
    public String toString()
    {
        return "";
    }
}

Employee类只是一个拥有getter和setter的数据持有者。

1 个答案:

答案 0 :(得分:-1)

我建议将员工类创建为最高级别的层次结构。管理员可以继承此Employee类。您可以在Manager类中拥有一个在他/她下工作的员工列表。

用于打印层次结构。您可以在经理的最高级别打电话打印,并递归打印所有推荐他的员工,并依次打印打印。

希望这会有所帮助。递归可能是打印整个层次结构的关键。