java递归

  1 package com.sun.test;
  2 
  3 import java.util.ArrayList;
  4 import java.util.List;
  5 
  6 
  7 /**
  8  * 递归
  9  *
 10  */
 11 public class Recursion {
 12 
 13     private String id;
 14     private String name;
 15     private Recursion parent;// 父单元格对象
 16     private List<Recursion> children = new ArrayList<Recursion>();// 子单元格集合
 17 
 18     public Recursion() {
 19     }
 20 
 21     public Recursion(String id) {
 22         this.id = id;
 23     }
 24     
 25     /**
 26      * 获得所有节点的id
 27      * @param r
 28      * @param rs
 29      */
 30     public static void getIds(Recursion r , List<String> rs) {
 31         if (r.getChildren() != null && r.getChildren().size() > 0) {
 32             rs.add(r.getId());
 33             for (Recursion recursion : r.getChildren()) {
 34                 getIds(recursion, rs);
 35             }
 36         } else {
 37             rs.add(r.getId());
 38         }
 39     }
 40     
 41     public static List<String> getIds(Recursion r) {
 42         List<String> rs = new ArrayList<>();
 43         if (r.getChildren() != null && r.getChildren().size() > 0) {
 44             rs.add(r.getId());
 45             for (Recursion recursion : r.getChildren()) {
 46                 rs.addAll(getIds(recursion));
 47             }
 48         } else {
 49             rs.add(r.getId());
 50         }
 51         return rs;
 52     }
 53     
 54     /**
 55      * 某个对象的子的最大宽度,也就是某个对象他最后一级的孩子节点的个数
 56      * @return
 57      */
 58     public int getRowNum() {
 59         int rowNum = children.size();
 60         for (Recursion hc : children) {
 61             if (hc.getRowNum() > 0) {
 62                 rowNum += hc.getRowNum() - 1;
 63             }
 64         }
 65         return rowNum;
 66     }
 67     
 68     public static void main(String[] args) {
 69         Recursion r = new Recursion("r");
 70         Recursion r1 = new Recursion("r1");
 71         Recursion r2 = new Recursion("r2");
 72         Recursion r1_1 = new Recursion("r1_1");
 73         Recursion r1_2 = new Recursion("r1_2");
 74         Recursion r1_3 = new Recursion("r1_3");
 75         Recursion r2_1 = new Recursion("r2_1");
 76         Recursion r2_2 = new Recursion("r2_2");
 77         Recursion r1_1_1 = new Recursion("r1_1_1");
 78         Recursion r1_1_2 = new Recursion("r1_1_2");
 79         Recursion r2_1_1 = new Recursion("r2_1_1");
 80         Recursion r2_1_2 = new Recursion("r2_1_2");
 81         Recursion r2_1_3 = new Recursion("r2_1_3");
 82         r.getChildren().add(r1);
 83         r.getChildren().add(r2);
 84         r1.getChildren().add(r1_1);
 85         r1.getChildren().add(r1_2);
 86         r1.getChildren().add(r1_3);
 87         r1_1.getChildren().add(r1_1_1);
 88         r1_1.getChildren().add(r1_1_2);
 89         r2.getChildren().add(r2_1);
 90         r2.getChildren().add(r2_2);
 91         r2_1.getChildren().add(r2_1_1);
 92         r2_1.getChildren().add(r2_1_2);
 93         r2_1.getChildren().add(r2_1_3);
 94         
 95         List<String> rs = new ArrayList<>();
 96         getIds(r, rs);
 97         for (String string : rs) {
 98             System.out.println(string);
 99         }
100         System.out.println("--------------------------");
101         List<String> rs1 = getIds(r);
102         for (String string : rs1) {
103             System.out.println(string);
104         }
105         System.out.println("--------------------------");
106         System.out.println(r.getRowNum());
107     }
108     
109     public String getId() {
110         return id;
111     }
112 
113     public void setId(String id) {
114         this.id = id;
115     }
116 
117     public String getName() {
118         return name;
119     }
120 
121     public void setName(String name) {
122         this.name = name;
123     }
124 
125     public Recursion getParent() {
126         return parent;
127     }
128 
129     public void setParent(Recursion parent) {
130         this.parent = parent;
131     }
132 
133     public List<Recursion> getChildren() {
134         return children;
135     }
136 
137     public void setChildren(List<Recursion> children) {
138         this.children = children;
139     }
140 
141 }

 

测试类结果为

 1 r
 2 r1
 3 r1_1
 4 r1_1_1
 5 r1_1_2
 6 r1_2
 7 r1_3
 8 r2
 9 r2_1
10 r2_1_1
11 r2_1_2
12 r2_1_3
13 r2_2
14 --------------------------
15 r
16 r1
17 r1_1
18 r1_1_1
19 r1_1_2
20 r1_2
21 r1_3
22 r2
23 r2_1
24 r2_1_1
25 r2_1_2
26 r2_1_3
27 r2_2
28 --------------------------
29 8
View Code

这里我会画出递归的流程,这样递归就显而易见了

 

前面是对实体的应用一般用在拥有主子关系的系统的。还有一种应用可能比较多见,那就是递归json,有个案例可以看一下,就是一个jsonArray字符串,我需要隐藏其中的某些节点,这里json的格式为含有chil

 1     /**
 2      * var nodes = [
 3      *    {id: "node1",
 4      *    children: [
 5      *        {id: "node1_1"},
 6      *        {id: "node1_2"}
 7      *    ]}
 8      *];
 9      * 递归JSONArray 移除不需要的id,如果子节点都被移除了,那么其父节点也要被移除,以此类推
10      * 这里默认过滤最底层节点,如果要过滤所有,只需要稍加改造
11      * @param arr json数组
12      * @param 拥有的id
13      * @return
14      */
15     @SuppressWarnings("unchecked")
16     public static int iterationMenu(JSONArray arr, List<String> ids) {
17         int f = 0;
18         for (Iterator<JSONObject> iterator = arr.iterator(); iterator.hasNext();) {
19             JSONObject level1Obj = (JSONObject) iterator.next();
20             if (level1Obj.containsKey("children")) {
21                 int j = iterationMenu(level1Obj.getJSONArray("children"), ids);
22                 if (level1Obj.containsKey("id") && ids.contains(level1Obj.getString("id"))) {
23                     j++;
24                 }
25                 if (j == 0) {
26                     iterator.remove();
27                 } else {
28                     f++;
29                 }
30             } else {
31                 if (level1Obj.containsKey("id")) {
32                     if (!ids.contains(level1Obj.getString("id"))) {
33                         iterator.remove();
34                     } else {
35                         f++;
36                     }
37                 }
38             }
39         }
40         return f;
41     }

只要了解了递归的执行顺序,那么上面的代码很容易理解!

 

posted @ 2016-06-14 20:37  user_孙  阅读(248)  评论(0编辑  收藏  举报