* 题目: * 1. 构建属性结构List<DemoNode> data,根据本包的data.png中数据结构图将数据存入data中(字就是nodeName) * 2. 将树形结构List<DemoNode>里面的元素全部遍历出来存放到List<String> list中,输出结果转换成字符串:粉粉碎机被粉碎机粉碎了怎么办 * 3. 将list里元素去重后存放到List<String> result,输出结果转换成字符串:粉碎机被了怎么办
主类:
/** * 考核点:List数据存储,回调函数,集合转字符串,元素去重 * <p> * 题目: * 1. 构建属性结构List<DemoNode> data,根据本包的data.png中数据结构图将数据存入data中(字就是nodeName) * 2. 将树形结构List<DemoNode>里面的元素全部遍历出来存放到List<String> list中,输出结果转换成字符串:粉粉碎机被粉碎机粉碎了怎么办 * 3. 将list里元素去重后存放到List<String> result,输出结果转换成字符串:粉碎机被了怎么办 */ public class Main01 { public static void main(String[] args) { // 树型数据存放 List<DemoNode> data = new ArrayList<>(); // 构建元素 DemoNode demoNode = generateNodeTree(); data.add(demoNode); // 遍历数据存放 List<String> eleList = new ArrayList<>(); getAllEle(eleList, data); System.out.println(String.join("", eleList)); // 粉粉碎机被粉碎机粉碎了怎么办 // 去重后元素存放 // method1: 使用set无法保证能按List顺序排序 Set<String> set = new HashSet<>(eleList); List<String> result1 = new ArrayList<>(set); System.out.println(String.join("", result1)); // 了么粉被碎怎机办 // method2: 使用List.contains方法判断重存放 List<String> result2 = new ArrayList<>(); for (String one : eleList) { if (!result2.contains(one)) { result2.add(one); } } System.out.println(String.join("", result2)); // 粉碎机被了怎么办 } /** * 遍历树形结构(按层级从小到大) * * @param eleList 存放节点名称元素集合 * @param nodeList 要遍历的元素集合 */ static void getAllEle(List<String> eleList, List<DemoNode> nodeList) { List<DemoNode> childList = new ArrayList<>(); for (DemoNode one : nodeList) { eleList.add(one.getNodeName()); if (one.getChild() != null && one.getChild().size() > 0) { childList.addAll(one.getChild()); } } // 下一级有子节点,开始下一级遍历 if (childList != null && childList.size() > 0) { getAllEle(eleList, childList); } } /** * 构建图片属性结构 * * @return */ private static DemoNode generateNodeTree() { /** 构造第三层元素并归属 **/ DemoNode node31 = new DemoNode("被"); List<DemoNode> node31Childs = addChildNodeList(new DemoNode("了"), new DemoNode("怎")); node31.setChild(node31Childs); DemoNode node33 = new DemoNode("碎"); List<DemoNode> node33Childs = addChildNodeList(new DemoNode("么"), new DemoNode("办")); node33.setChild(node33Childs); /** 构造第二层元素并归属 **/ DemoNode node21 = new DemoNode("粉"); List<DemoNode> node21Childs = addChildNodeList(node31, new DemoNode("粉")); node21.setChild(node21Childs); DemoNode node22 = new DemoNode("碎"); List<DemoNode> node22Childs = addChildNodeList(node33, new DemoNode("机")); node22.setChild(node22Childs); DemoNode node23 = new DemoNode("机"); List<DemoNode> node23Childs = addChildNodeList(new DemoNode("粉"), new DemoNode("碎")); node23.setChild(node23Childs); /** 构造第一层元素并归属 **/ DemoNode nodeRoot = new DemoNode("粉"); List<DemoNode> nodeRootChilds = addChildNodeList(node21, node22, node23); nodeRoot.setChild(nodeRootChilds); return nodeRoot; } /** * 组装子节点 * * @param childNodes * @return */ private static List<DemoNode> addChildNodeList(DemoNode... childNodes) { List<DemoNode> list = new ArrayList<>(); for (DemoNode one : childNodes) { list.add(one); } return list; } }
节点类:
/** * 节点数据 */ public class DemoNode { private String NodeName; //节点名称 private List<DemoNode> child; //子节点 public String getNodeName() { return NodeName; } public void setNodeName(String nodeName) { NodeName = nodeName; } public List<DemoNode> getChild() { return child; } public void setChild(List<DemoNode> child) { this.child = child; } public DemoNode(String nodeName) { NodeName = nodeName; } public DemoNode() { } }
标签:01,DemoNode,粉碎机,DEMO,List,eleList,new,String From: https://www.cnblogs.com/cola-zhou/p/18258311