计算员工的重要度

时间:2019-11-07
本文章向大家介绍计算员工的重要度,主要包括计算员工的重要度使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
/**
 * 深度遍历的一个简单例子
 *
 */
public class EmployeeImportance {
    
    private static int res = 0;
    private static Employee employee;
    public static void main(String[] args) {
        Employee e1 = new Employee(1, 5, Arrays.asList(2,3));
        Employee e2 = new Employee(2, 3, null);
        Employee e3 = new Employee(3, 3, null);
        int total = getImportance(Arrays.asList(e1,e2,e3), 1);
        System.out.println(total);
        
    }
    
    
    public static int getImportance(List<Employee> emps, int id) {
        for(Employee emp : emps) {
            if(emp.id == id) {
                employee = emp;
                res += emp.value;
                break;
            }
        }
        List<Integer> list = employee.subordinates;
        if(list != null) {
            for(Integer i : list) {
                getImportance(emps, i);
            }
        }
        return res;
    }
    
}

class Employee {
    
    int id;
    int value;
    List<Integer> subordinates;

    public Employee(int id, int value, List<Integer> subordinates) {
        super();
        this.id = id;
        this.value = value;
        this.subordinates = subordinates;
    }
}

原文地址:https://www.cnblogs.com/moris5013/p/11810728.html