zoukankan      html  css  js  c++  java
  • 递归方法(树状结构显示样板): 用于部门列表上下级的循环,以及图书分类中上下级循环分别显示的情况

    package cn.itcast.oa.test;
    
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.LinkedHashSet;
    import java.util.List;
    import java.util.Set;
    
    import org.junit.Test;
    
    import cn.itcast.oa.domain.Department;
    
    /**
     * 说明:不能使用多层循环的方式,因为需要能支持任意层。
     */
    public class TreeViewPractice {
    
    	/**
    	 * 练习一:打印所有顶层部门及其子孙部门的信息(名称) 提示:假设有一个 打印部门树 的信息 的方法
    	 * 
    	 * 要求打印如下效果:
    	 * 
    	 * <pre>
    	 * 市场部
    	 * 宣传部
    	 * 业务部
    	 * 业务一部
    	 * 业务二部
    	 * 开发部
    	 * 开发一部
    	 * 开发二部
    	 * </pre>
    	 */
    	@Test
    	public void printAllDepts_1() {
    		List<Department> topList = findTopLevelDepartmentList();
    
    		// // 方式一
    		// for (Department top : topList) {
    		// showTree(top);
    		// }
    
    		// 方式二
    		showTreeList(topList);
    	}
    
    	// 显示一颗树的信息
    	private void showTree(Department top) {
    		// 顶点
    		System.out.println(top.getName());
    		// 子树
    		for (Department child : top.getChildren()) {
    			showTree(child); //内部调用自身方法再次对下一层级显示
    		}
    	}
    
    	// 显示多颗树的信息
    	private void showTreeList(Collection<Department> topList) {
    		for(Department top : topList){
    			// 顶点
    			System.out.println(top.getName());
    			// 子树
    			showTreeList(top.getChildren());//内部调用自身方法再次对下一层级显示
    		}
    	}
    
    	/**
    	 * 练习二:打印所有顶层部门及其子孙部门的信息(名称),用不同的缩进表示层次(使用全角空格)。<br>
    	 * 子部门的名称前比上级部门多一个空格,最顶层部门的名字前没有空格。 提示:假设有一个打印部门集合中所有部门信息的方法
    	 * 
    	 * 要求打印如下效果:
    	 * 
    	 * <pre>
    	 * ┣市场部
    	 *    ┣宣传部
    	 *    ┣业务部
    	 *       ┣业务一部
    	 *       ┣业务二部
    	 * ┣开发部
    	 *    ┣开发一部
    	 *    ┣开发二部
    	 * </pre>
    	 */
    	@Test
    	public void printAllDepts_2() {
    		List<Department> topList = findTopLevelDepartmentList();
    
    		showTreeList_2(topList,  "┣");//需要全角符号┣
    	}
    	
    	// 显示多颗树的信息
    	private void showTreeList_2(Collection<Department> topList, String prefix) {
    		for(Department top : topList){
    			// 顶点
    			System.out.println( prefix  + top.getName());
    			// 子树
    			showTreeList_2(top.getChildren(), " " + prefix);// 这种方式,每一次的重复调用该方法,就会累加上两个空格....注:全角下空格
    		}
    	}
    
    	/**
    	 * 结构如下:
    	 * 
    	 * <pre>
    	 * ┣市场部
    	 *    ┣宣传部
    	 *    ┣业务部
    	 *       ┣业务一部
    	 *       ┣业务二部
    	 * ┣开发部
    	 *    ┣开发一部
    	 *    ┣开发二部
    	 * </pre>
    	 * 
    	 * @return 所有最顶层的部门的列表
    	 */
    	public static List<Department> findTopLevelDepartmentList() {
    		Department dept_1_1 = new Department();
    		dept_1_1.setId(new Long(11));
    		dept_1_1.setName("宣传部");
    
    		Department dept_1_2 = new Department();
    		dept_1_2.setId(new Long(12));
    		dept_1_2.setName("业务部");
    
    		Department dept_1_2_1 = new Department();
    		dept_1_2_1.setId(new Long(121));
    		dept_1_2_1.setName("业务一部");
    
    		Department dept_1_2_2 = new Department();
    		dept_1_2_2.setId(new Long(122));
    		dept_1_2_2.setName("业务二部");
    
    		dept_1_2_1.setParent(dept_1_2);
    		dept_1_2_2.setParent(dept_1_2);
    		Set<Department> children_0 = new LinkedHashSet<Department>();
    		children_0.add(dept_1_2_1);
    		children_0.add(dept_1_2_2);
    		dept_1_2.setChildren(children_0);
    
    		// ================================
    
    		Department dept_1 = new Department();
    		dept_1.setId(new Long(1));
    		dept_1.setName("市场部");
    
    		dept_1_1.setParent(dept_1);
    		dept_1_2.setParent(dept_1);
    		Set<Department> children_1 = new LinkedHashSet<Department>();
    		children_1.add(dept_1_1);
    		children_1.add(dept_1_2);
    		dept_1.setChildren(children_1);
    
    		// ---
    
    		Department dept_2_1 = new Department();
    		dept_2_1.setId(new Long(21));
    		dept_2_1.setName("开发一部");
    
    		Department dept_2_2 = new Department();
    		dept_2_2.setId((new Long(22)));
    		dept_2_2.setName("开发二部");
    
    		Department dept_2 = new Department();
    		dept_2.setId(new Long(2));
    		dept_2.setName("开发部");
    
    		dept_2_1.setParent(dept_2);
    		dept_2_2.setParent(dept_2);
    		Set<Department> children_2 = new LinkedHashSet<Department>();
    		children_2.add(dept_2_1);
    		children_2.add(dept_2_2);
    		dept_2.setChildren(children_2);
    
    		// ---
    
    		List<Department> depts = new ArrayList<Department>();
    		depts.add(dept_1);
    		depts.add(dept_2);
    		return depts;
    	}
    
    }
    
    I'm falling off the sky all alone.The courage inside is gonna break the fall. Nothing can dim my light within. I am That I am 程序 = 数据结构 + 算法
  • 相关阅读:
    LVDS DP等显示器接口简介
    Eclipse智能感知及快捷键
    Error deploying web application directory D:apache-tomcat-8.0.26webappsservlet
    把tomcat默认的8080段口改成80端口
    警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server did not find a matching property.
    软件危机
    SQL Server——热备份
    mysql热备份
    Oracle——热备份
    Nodejs基础
  • 原文地址:https://www.cnblogs.com/IamThat/p/2886303.html
Copyright © 2011-2022 走看看