【发布时间】:2017-12-09 22:59:52
【问题描述】:
所以我有这个 Person 对象。每个人都有人对象列表等等..
public class Person {
....
private List<Person> people = new ArrayList<>();
....
public List<Person> getPeople() {
return people;
}
public void setPeople(List<Person> people) {
this.people = people;
}
我已经使用以下代码得到了最大部门的答案,答案是我得到的任何 int 减去 1
public static int maxDepth(Person p) {
int maxChildrenDepth = 0;
for (Person c: p.getPeople()) {
maxChildrenDepth = Math.max(maxChildrenDepth, maxDepth(c));
}
return 1 + maxChildrenDepth;
}
所以如果我将 Person 对象和 int 传递给方法,比如说 getPersonLevel(List allPerson, 1) ,我应该得到 List 内所有蓝色框的 Person 对象,如果我输入 2,我应该得到所有对象在红色框的列表中等等,具体取决于 int 参数..我如何做到这一点?任何帮助表示赞赏。
【问题讨论】:
标签: java oop collections java-8 depth-first-search