62,614
社区成员
发帖
与我相关
我的任务
分享
public class Group{
private String id;
private String parentId;//父ID
public String getId(){return id;}
public void setId(String id){this.id=id;}
public String getParentId(){return parentId;}
public void setParentId(String parentId){this.parentId=parentId;}
}
import java.util.ArrayList;
import java.util.List;
public class Test
{
public static void main(String[] args)
{
Group g1 = new Group(1,0);
Group g2 = new Group(2,1);
Group g3 = new Group(3,2);
List<Group> list = new ArrayList<Group>();
list.add(g1);
list.add(g2);
list.add(g3);
Test.seach(0, list);
}
public static void seach(Integer indexId,List<Group> list){
for (Group group : list)
{
if(group.getParentId() == indexId){
System.out.println(group.getId());
seach(group.getId(), list);
}
}
}
}
class Group{
private Integer id;
private Integer parentId;//父ID
public Group(Integer id, Integer parentId)
{
this.id = id;
this.parentId = parentId;
}
public Integer getId()
{
return id;
}
public void setId(Integer id)
{
this.id = id;
}
public Integer getParentId()
{
return parentId;
}
public void setParentId(Integer parentId)
{
this.parentId = parentId;
}
}