站内搜索: 请输入搜索关键词

当前页面: 开发资料首页JSP 专题用递归遍历树形结构节点时出错,请帮忙看看!~(没分了,只有10分了)

用递归遍历树形结构节点时出错,请帮忙看看!~(没分了,只有10分了)

摘要: 用递归遍历树形结构节点时出错,请帮忙看看!~(没分了,只有10分了)


cy_ID是记录编号,cy_PID是父节点编号,cy_Child是子节点个数,ConnectionBean,是连库和数据库操作的bean,arr是个全局变量
下面是代码:
List arr = new ArrayList();
public void MoveType(String id1 /*要移动的分类的id*/, String id2 /*被作为父类的类的id*/) {

tb.FindAll(Integer.parseInt(id1));
for (int i = 0; i < arr.size(); i++) {
System.out.println(arr.get(i));
}

}
public void FindAll(int id) {
try {
ConnectionBean cb = new ConnectionBean();
ResultSet rs = null;
TypeBean tb = new TypeBean();
arr.add(String.valueOf(id));
int Child = 0;
rs = cb.dbSelect(
"select cy_Child from cy_articleclass where cy_ID=" + id +
"");
while (rs.next()) {
Child = rs.getInt(1);
}
if (Child != 0) {
rs = cb.dbSelect(
"select cy_ID from cy_articleclass where cy_PID=" + id +
"");

while (rs.next()) {

tb.FindAll(rs.getInt(1));

}
}
} catch (Exception e) {
e.printStackTrace();
}
}
下面是出错的提示代码:
type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Servlet execution threw an exception


root cause

java.lang.StackOverflowError


note The full stack trace of the root cause is available in the Apache Tomcat/5.0.27 logs.
请帮忙看看为什么会出这样的错误!~


少帖了一个全局变量,以下也是在代码中的:
TypeBean tb = new TypeBean();


可以肯定是你的程序出现了死循环,tb不断调用findAll(),直至资源耗尽


确实如此,我感觉也应该是死循环,可是,我没看出来造成死循环的原因阿,哪位高人能帮忙瞧瞧啊,拜谢啦~~~~


我在程序里用 if (Child != 0) 作为条件判断是否继续调用自身阿,为什么还会有死循环呢?



那要看你articleclass 表中数据之间的关系是什么样的,你这个树是否有重复相同的结点


构成死循环有可能是数据库里数据不对!

比如节点A 的cy_PID设置为B ,而节点B的cy_PID又指向A!!

while (rs.next()) {
//这里加个验证:rs.getInt(1)的id号不在arr 列表里面。 ps:这种情况用hashMap比较好
if(rs.getInt(1) NOT IN arr )
tb.FindAll(rs.getInt(1));

}


数据库里的数据如果拿出来根本不是可遍历树


↑返回目录
前一篇: 为什么要抛出例外?
后一篇: 100分求,STRUTS项目实践方面的书籍