java问题(求助大神,急需帮助)

shakar 2013-12-26 12:57:26


因为很急,能不能直接上代码?
...全文
340 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
duwei1990 2013-12-29
  • 打赏
  • 举报
回复
一种是根据父节点找所有子节点,另一种是相反的,楼主是这个意思么?
别闹腰不好 2013-12-26
  • 打赏
  • 举报
回复
public class MyBean{ String id; String name; String parent; List<MyBean> children; } 这个应该是你的bean类型, public void outBean(List<MyBean> b){ for(MyBean one:b){ System.out.print(b.getId()+b.getName()+b.getParent()); if(b.getChildren()!=null&&b.getChildren().size()>0) outBean(b.getChildren()); } } 这个就可以,你好好看看。另外 你举的例子好像根本不能发生,生成bean的时候,是从最高节点开始的。 //C,D //A,C 你中意思是c里包含D ,A里包含c。 其实在递归生BEAN时候,A的子节点集合里包含C。之后c的子节点结合包含D ,只会有这一种情况
shakar 2013-12-26
  • 打赏
  • 举报
回复
引用 13 楼 u012463264 的回复:
[quote=引用 12 楼 shakar 的回复:] [quote=引用 10 楼 u012463264 的回复:] [quote=引用 9 楼 shakar 的回复:] [quote=引用 7 楼 u012463264 的回复:] public class MyBean{ String id; String name; String parent; List<MyBean> children; } 这个应该是你的bean类型, public void outBean(List<MyBean> b){ for(MyBean one:b){ System.out.print(b.getId()+b.getName()+b.getParent()); if(b.getChildren()!=null&&b.getChildren().size()>0) outBean(b.getChildren()); } }
做了好久了,都搞不定,能帮帮我吗[/quote] 可以 啊 主要你说不明白啊 [/quote] 顺序是从上到下,先说第一种,A因为是在第一个所以就变成了id=11,name=A,parent为1. B在第二个就变成了id=22,name=B,parent=1. 第三个有关系是C属于A的,A又在前面出现过了,所以C就为id=1111,name=C,parent=11.第四个D是属于C的C在前面出现过了,所以D为id=111111,name=D,parent=1111 第二中,输出结果改一下 //C,D //A,C //B //A //输出结果 //ID NAME Parent //1 o //11 C 1 //1111 D 11 //22 A 1 //2211 C 22 //33 B 1[/quote] 你说的这些我都明白 ,主要你的封装的类是怎么设计的啊 是每一行就是一个bean对象 ,还是那种每个对象里包含子的节点?[/quote] 对象里包含子的节点
别闹腰不好 2013-12-26
  • 打赏
  • 举报
回复
引用 12 楼 shakar 的回复:
[quote=引用 10 楼 u012463264 的回复:] [quote=引用 9 楼 shakar 的回复:] [quote=引用 7 楼 u012463264 的回复:] public class MyBean{ String id; String name; String parent; List<MyBean> children; } 这个应该是你的bean类型, public void outBean(List<MyBean> b){ for(MyBean one:b){ System.out.print(b.getId()+b.getName()+b.getParent()); if(b.getChildren()!=null&&b.getChildren().size()>0) outBean(b.getChildren()); } }
做了好久了,都搞不定,能帮帮我吗[/quote] 可以 啊 主要你说不明白啊 [/quote] 顺序是从上到下,先说第一种,A因为是在第一个所以就变成了id=11,name=A,parent为1. B在第二个就变成了id=22,name=B,parent=1. 第三个有关系是C属于A的,A又在前面出现过了,所以C就为id=1111,name=C,parent=11.第四个D是属于C的C在前面出现过了,所以D为id=111111,name=D,parent=1111 第二中,输出结果改一下 //C,D //A,C //B //A //输出结果 //ID NAME Parent //1 o //11 C 1 //1111 D 11 //22 A 1 //2211 C 22 //33 B 1[/quote] 你说的这些我都明白 ,主要你的封装的类是怎么设计的啊 是每一行就是一个bean对象 ,还是那种每个对象里包含子的节点?
shakar 2013-12-26
  • 打赏
  • 举报
回复
引用 10 楼 u012463264 的回复:
[quote=引用 9 楼 shakar 的回复:]
[quote=引用 7 楼 u012463264 的回复:]
public class MyBean{
String id;
String name;
String parent;
List<MyBean> children;
}
这个应该是你的bean类型,

public void outBean(List<MyBean> b){
for(MyBean one:b){
System.out.print(b.getId()+b.getName()+b.getParent());
if(b.getChildren()!=null&&b.getChildren().size()>0)
outBean(b.getChildren());
}

}


做了好久了,都搞不定,能帮帮我吗[/quote]

可以 啊 主要你说不明白啊 [/quote]



顺序是从上到下,先说第一种,A因为是在第一个所以就变成了id=11,name=A,parent为1. B在第二个就变成了id=22,name=B,parent=1. 第三个有关系是C属于A的,A又在前面出现过了,所以C就为id=1111,name=C,parent=11.第四个D是属于C的C在前面出现过了,所以D为id=111111,name=D,parent=1111


第二中,输出结果改一下
//C,D
//A,C
//B
//A
//输出结果
//ID NAME Parent
//1 o
//11 C 1
//1111 D 11
//22 A 1
//2211 C 22
//33 B 1
别闹腰不好 2013-12-26
  • 打赏
  • 举报
回复
引用 10 楼 u012463264 的回复:
[quote=引用 9 楼 shakar 的回复:] [quote=引用 7 楼 u012463264 的回复:] public class MyBean{ String id; String name; String parent; List<MyBean> children; } 这个应该是你的bean类型, public void outBean(List<MyBean> b){ for(MyBean one:b){ System.out.print(b.getId()+b.getName()+b.getParent()); if(b.getChildren()!=null&&b.getChildren().size()>0) outBean(b.getChildren()); } }
做了好久了,都搞不定,能帮帮我吗[/quote] 可以 啊 主要你说不明白啊 [/quote] 我已经给你写了 ,你吧Bean加上set和get方法就行了 ,那个方法是递归输出的 , 主要你要查询数据 ,放到BEAN里 。
别闹腰不好 2013-12-26
  • 打赏
  • 举报
回复
引用 9 楼 shakar 的回复:
[quote=引用 7 楼 u012463264 的回复:] public class MyBean{ String id; String name; String parent; List<MyBean> children; } 这个应该是你的bean类型, public void outBean(List<MyBean> b){ for(MyBean one:b){ System.out.print(b.getId()+b.getName()+b.getParent()); if(b.getChildren()!=null&&b.getChildren().size()>0) outBean(b.getChildren()); } }
做了好久了,都搞不定,能帮帮我吗[/quote] 可以 啊 主要你说不明白啊
shakar 2013-12-26
  • 打赏
  • 举报
回复
引用 7 楼 u012463264 的回复:
public class MyBean{ String id; String name; String parent; List<MyBean> children; } 这个应该是你的bean类型, public void outBean(List<MyBean> b){ for(MyBean one:b){ System.out.print(b.getId()+b.getName()+b.getParent()); if(b.getChildren()!=null&&b.getChildren().size()>0) outBean(b.getChildren()); } }
做了好久了,都搞不定,能帮帮我吗
shakar 2013-12-26
  • 打赏
  • 举报
回复
引用 7 楼 u012463264 的回复:
public class MyBean{ String id; String name; String parent; List<MyBean> children; } 这个应该是你的bean类型, public void outBean(List<MyBean> b){ for(MyBean one:b){ System.out.print(b.getId()+b.getName()+b.getParent()); if(b.getChildren()!=null&&b.getChildren().size()>0) outBean(b.getChildren()); } }
能不能帮忙,全部都写出来?
别闹腰不好 2013-12-26
  • 打赏
  • 举报
回复
public class MyBean{ String id; String name; String parent; List<MyBean> children; } 这个应该是你的bean类型, public void outBean(List<MyBean> b){ for(MyBean one:b){ System.out.print(b.getId()+b.getName()+b.getParent()); if(b.getChildren()!=null&&b.getChildren().size()>0) outBean(b.getChildren()); } }
teemai 2013-12-26
  • 打赏
  • 举报
回复
你倒是把这个关系说清楚啊?
别闹腰不好 2013-12-26
  • 打赏
  • 举报
回复
引用 3 楼 shakar 的回复:
[quote=引用 2 楼 u012463264 的回复:] 吧数据发出来 吧 我给你写 。
List<String> list = new ArrayList<String>(); list.add("a"); list.add("b"); list.add("a,c"); list.add("a,c,d"); 循环它,然后变成 //id name parent //1 O //11 A 1 //22 B 1 //1111 C 11 //111111 D 1111[/quote] 我看了你的另一个帖子 ,感觉你这数据不对 , 应该是一个对象里含有list 。才对。
shakar 2013-12-26
  • 打赏
  • 举报
回复
引用 2 楼 u012463264 的回复:
吧数据发出来 吧 我给你写 。
题目的结果有点问题,刚发给你的结果是没问题的
shakar 2013-12-26
  • 打赏
  • 举报
回复
引用 2 楼 u012463264 的回复:
吧数据发出来 吧 我给你写 。
List<String> list = new ArrayList<String>(); list.add("a"); list.add("b"); list.add("a,c"); list.add("a,c,d"); 循环它,然后变成 //id name parent //1 O //11 A 1 //22 B 1 //1111 C 11 //111111 D 1111
别闹腰不好 2013-12-26
  • 打赏
  • 举报
回复
吧数据发出来 吧 我给你写 。
tony4geek 2013-12-26
  • 打赏
  • 举报
回复
看不懂啊。哎。

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧