递归是一个非常有用的知识点。写点实例帮助自己记忆
中间有过程代码
首先一个javapojo类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
package com.qcf.po; import java.util.HashSet; import java.util.Set; public class Depart { private long id; private String name; private String destion; //用户 Set<User> users= new HashSet<User>(); //子类部门 Set<Depart> departs= new HashSet<Depart>(); //父类部门 private Depart depart; public long getId() { return id; } public void setId( long id) { this .id = id; } public String getName() { return name; } public void setName(String name) { this .name = name; } public Set<User> getUsers() { return users; } public void setUsers(Set<User> users) { this .users = users; } public Set<Depart> getDeparts() { return departs; } public void setDeparts(Set<Depart> departs) { this .departs = departs; } public Depart getDepart() { return depart; } public void setDepart(Depart depart) { this .depart = depart; } public String getDestion() { return destion; } public void setDestion(String destion) { this .destion = destion; } } |
测试代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
package com.qcf.util; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.util.List; import java.util.Set; import org.aspectj.weaver.patterns.ThisOrTargetAnnotationPointcut; import com.qcf.po.Depart; public class PreDepart { /** * 结构 * ┝开发部 * ┝开发一部 * ┝开发一组 * ┝开发二组 * ┝开发二部 * ┝开发三组 * ┝开发四组 * ┝销售部 * ┝销售一部 * ┝销售二部 */ public static List<Depart> findDepartTopList(){ //顶级 Depart depart1= new Depart(); depart1.setName( "开发部" ); //二级 Depart depart1_1= new Depart(); depart1_1.setName( "开发一部" ); //三级 Depart depart1_1_1= new Depart(); depart1_1_1.setName( "开发一组" ); Depart depart1_1_2= new Depart(); depart1_1_2.setName( "开发二组" ); Set<Depart> departs1_1= new HashSet<Depart>(); departs1_1.add(depart1_1_1); departs1_1.add(depart1_1_2); depart1_1.setDeparts(departs1_1); Depart depart1_2= new Depart(); depart1_2.setName( "开发二部" ); Depart depart1_2_1= new Depart(); depart1_2_1.setName( "开发一组" ); Depart depart1_2_2= new Depart(); depart1_2_2.setName( "开发二组" ); Set<Depart> departs1_2= new HashSet<Depart>(); departs1_2.add(depart1_1_1); departs1_2.add(depart1_1_2); depart1_2.setDeparts(departs1_1); Set<Depart> departs1= new HashSet<Depart>(); departs1.add(depart1_1); departs1.add(depart1_2); depart1.setDeparts(departs1); //顶级 Depart depart2= new Depart(); depart2.setName( "销售部" ); //二级 Depart depart2_1= new Depart(); depart2_1.setName( "销售一部" ); Depart depart2_2= new Depart(); depart2_2.setName( "销售二部" ); Set<Depart> departs= new HashSet<Depart>(); departs.add(depart2_1); departs.add(depart2_2); depart2.setDeparts(departs); List<Depart> list= new ArrayList<Depart>(); list.add(depart1); list.add(depart2); return list; } public static void main(String[] args) { List<Depart> list=findDepartTopList(); // for (Depart depart : list) { // getAllDepartName(depart); // } getAllDepartName_2(list, "=" ); } /**将所有的部门打印出来*/ public static void getAllDepartName(Depart depart){ System.out.println(depart.getName()); for (Depart chirden : depart.getDeparts()) { getAllDepartName(chirden); } } /**将所有的部门打印出来*/ public static void getAllDepartName_2(Collection<Depart> list,String pre){ for (Depart depart2 : list) { System.out.println( pre +depart2.getName()); getAllDepartName_2(depart2.getDeparts(), " " +pre); } } } |
总结
以上就是本文关于Java递归算法遍历部门代码示例的全部内容,希望对大家有所帮助。如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!
原文链接:http://www.cnblogs.com/quchengfeng/p/4155419.html