1.一个树形结构对象,父子节点包含
public class TreeNode{
private int id; private String text; private List<TreeNode> childrenTreeNode ;}2.通过sql查询生成List<TreeNode>对象3.由于层级不确定,所以打算递归生成为List<List<TreeNode>>这种类似二维数组的结构到前台遍历,要按层级,第一层放第一个List,第二层放第二List,想用递归生成,但写了好久都不对,哪位大神帮帮忙,看看
public class TreeNode{
private int id; private String text; private List<TreeNode> childrenTreeNode ;}2.通过sql查询生成List<TreeNode>对象3.由于层级不确定,所以打算递归生成为List<List<TreeNode>>这种类似二维数组的结构到前台遍历,要按层级,第一层放第一个List,第二层放第二List,想用递归生成,但写了好久都不对,哪位大神帮帮忙,看看
解决方案 »
- Spring + Ibatis 事务为何不回滚?
- 我想问问spring+hibernate这个架构最近很"红"
- struts问题:给点提示就给分数--在线等待中
- 感谢各位高手的帮忙! 现在还有一个关于soap消息的问题:在服务端定义返回的各个参数的名称、类型。
- struts中新增,编辑页面是如何实现的
- ------向jtable中添加滚动条的问题------
- 郁闷,我的Tomcat在eclipse中无法启动!
- ssh中jsp页面的dataGrid无法获得数据
- 学完ssh,和ssi以后,想学点其他的东西了,大神们有什么推荐的吗
- SQL怎样大批量把一张表的某个用户的记录总数当做值插入到另外一张表的某个字段
- eclipse maven项目未知异常,有木有知道怎么解决的
- 跪求一个ssm网店系统
import java.util.List;
import java.util.Stack;public class Test {
/**
* @param sources 通过sql查询生成List<TreeNode>对象
*/
public List<List<TreeNode>> test(List<TreeNode> sources) {
List<List<TreeNode>> result = new ArrayList<>();
for (TreeNode treeNode : sources) {
List<TreeNode> nodes = getChildNodes(treeNode);
if (nodes != null && nodes.size() > 0) {
result.add(nodes);
}
}
return result;
} /**
* 前序遍历树,获得所有子节点
*/
public static List<TreeNode> getChildNodes(TreeNode node) {
if (node == null) {
return null;
}
List<TreeNode> result = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();// 创建栈
stack.push(node);// 根节点入栈
while (!stack.isEmpty()) {
TreeNode current = stack.pop();// 节点出栈
result.add(current);
List<TreeNode> children = current.getChildrenTreeNode();// 获得节点的所有子节点
if (children == null || children.size() == 0) {
continue;
}
for (TreeNode child : children) {// 当前节点current的所有子节点入栈
if (child != null) {
stack.push(child);
}
}
}
return result;
}
}class TreeNode {
private int id; private String text; private List<TreeNode> childrenTreeNode; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getText() {
return text;
} public void setText(String text) {
this.text = text;
} public List<TreeNode> getChildrenTreeNode() {
return childrenTreeNode;
} public void setChildrenTreeNode(List<TreeNode> childrenTreeNode) {
this.childrenTreeNode = childrenTreeNode;
}}