传智播客学习笔记6.3

来源:互联网 发布:js文件上传插件 编辑:程序博客网 时间:2024/06/08 08:16

 传智播客学习笔记6.3

public static void main(String[] args) {
  // List<Department> depts = preDepts();

  /**
   * <pre>
   * 打印如下效果(不要求顺序):
   *
   * -传智播客
   * |
   * |-- 市场部
   * |     |
   * |     |-- 市场1部
   * |     |-- 市场2部
   * |
   * |---开发部
   * |     |
   * |     |-- 开发1部
   * |     |-- 开发2部
   * </pre>
   */

  Set<Department> depts = preDepts();
  for (Department dept : depts) {
   printDeptInfo(dept);
  }
 }

 public static void printDeptInfo(Department dept) {
  System.out.println(dept.getName());
  for (Department d : dept.getChildren()) {
   printDeptInfo(d);
  }
 }

 // ========= 学生2 *

 @Test
 public void test2() {
  Set<Department> depts = preDepts();
  print(depts);
 }

 public static void print(Set<Department> depts) {
  for (Department dept : depts) {
   System.out.println(dept.getName());
   print(dept.getChildren());
  }
 }

 

递归显示部门的信息。

 

 public static void main(String[] args) {
  Set<Department> depts = preDepts();//获取所有顶级部门
//  System.out.println(depts);
  for(Department dept : depts){
   generateAddCode("tree", dept);
  }
 }
 
 public static void generateAddCode(String name,Department dept){
  System.out.println("var item" + dept.getId() + " = " + name + ".add(new WebFXTreeItem('" + dept + "'));");//生成树
  
  for(Department d : dept.getChildren()){
   generateAddCode("item" + dept.getId(), d);
  }
  
  
 }


!!递归!

<html:multibox/>

返回类型为集合的方法,如果需要返回空,不要直接返回null,要返回一个空集合

疑问:空集合使用的时候不会报空指针异常么?


<fieldset>
 <legend></legend>
</fieldset>

原创粉丝点击