JiveJdon Community Forums
在线536人   首页   主题表   培训咨询   标签   精华   查搜   注册    登陆 RSS
首页 » 论坛 » 设计模式、框架和架构
???en_US.forumThreadPrev.name??? 上一主题
  Go back to the topic 返回本主题   Go back to the topic listing返回主题列表
???en_US.forumThreadNext.name??? 下一主题
Go 总共有 2 回复 / 1
 发表新帖子   回复该主题贴
foxprince

悄悄话
发表文章: 13
注册时间: 2002年09月05日 17:17
用Compsite模式实现树形结构时的堆栈溢出异常 2002年11月21日 17:01 到本帖网址 加入本帖到收藏夹 发送到手机 回复该主题
标签列表
我用Composite模式实现树形结构时碰到如下问题:在初始化树时,如果遍历非根节点,并向其上添加孩子节点时总会出现堆栈溢出的错误,具体代码如下:

Public class Composite {
......
protected int id;
protected List children = new ArrayList();

public int getID() {
return this.id;
}

public Iterator getChildren() {
return this.children.iterator();
}

public int getSize() {
return this.children.size();
}

public void add(Composite c) {
try {
this.children.add(c);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}


/**
* Adds a child to the tree.
*
* @param parent the parent to add the new value to.
* @param child new value to add to the tree.
*/
public void addChild(Composite parent, Composite child) {
int parentID = parent.getID();
int childID = child.getID();

if(this.getID() == parentID) {
this.add(child);
}
else {
if(getSize()<1) {
throw new IllegalArgumentException("Parent key " + parentID +
" not found when adding child " + childID + ".");
}
else {
Iterator it = this.getChildren();
Composite node = null;
while(it.hasNext()) {
node = (Composite)it.next();
addChild(node,child);
}
}
}
}
......
}

当执行初始化调用addChild()方法时,总会出现如下异常

java.lang.StackOverflowError, stack: java.lang.StackOverflowError
at java.util.AbstractList$Itr.<init>(AbstractList.java:391)
at java.util.AbstractList$Itr.<init>(AbstractList.java:391)
at java.util.AbstractList.iterator(AbstractList.java:333)
at com.si_tech.nsd.util.tree.Composite.getChildren(Composite.java:101)
at com.si_tech.nsd.util.tree.Composite.addChild(Composite.java:168)

也就是说,对非根节点调用getChildren()方法时发生了堆栈溢出错误,这是为什么???
foxprince

悄悄话
发表文章: 13
注册时间: 2002年09月05日 17:17
真沮丧,问题已经找到拉,原来是某个方法调用错了 2002年11月22日 09:10 到本帖网址 加入本帖到收藏夹 发送到手机 回复该主题
大家发现了么?
banq

悄悄话
发表文章: 9482
注册时间: 2002年08月03日 17:08
Re: 真沮丧,问题已经找到拉,原来是某个方法调用错了 2002年11月22日 11:27 到本帖网址 加入本帖到收藏夹 发送到手机 回复该主题
addchild要catch exception
这属于运行的error,你需要具体自己调试。
这个主题有 2 回复 / 1Go
???en_US.forumThreadPrev.name??? 上一主题
  Go back to the topic 返回本主题   Go back to the topic listing返回主题列表    返回页首返回页首
???en_US.forumThreadNext.name??? 下一主题
热点TAG: AOP cache 缓存 DDD EJB 集群 设计模式 Hibernate IOC JiveJdon OO RBAC Seam Spring Struts
正在读取,请等待...
google yahoo 新浪ViVi 365Key网摘 天极网摘 CSDN网摘 添加到百度搜藏 POCO网摘 博采网摘
查询本论坛内 回复超过的热门帖子
     回复该主题贴
标题
 
粗体 斜体 下划线 插入图片 插入代码 插入url链接 插入附件
内容
  发贴前查询 标签列表勿重复发表问题

RSS 手机阅读 add to google add to yahoo
解惑之道在J道 ,打造中国最具影响力的的企业软件社区
OpenSource JIVEJDON v3.0 Powered by JdonFramework Code © 2002-08 jdon.com
anti spam