该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
运用递归算法,得出父节点 子节点集合 的测试代码:
import java.util.Iterator;import java.util.List;import java.util.Set;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.sl.goods.treemanager.services.TreeManagerService;
import com.sl.goods.treemanager.services.TreeManagerServiceImpl;
import com.sl.goods.vo.Tree;
public class TreeTest {
/尝试利用递归算法///递归父类
public static void parent(Tree tree){
if(tree.getParent()==null)
{
return;
}
else{
System.out.println(tree.getName());
parent(tree.getParent());
}}
递归子类
public static void children(Tree tree){
if(tree.getChildren().size()==0)
{return ;}
else{
System.out.println(tree.getChildren().size());
List list=tree.getChildren();
for(int i=0;i
tree=(Tree)list.get(i);
}
children(tree);
}
}
public static void main(String[] args) {
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContext-common.xml");
TreeManagerService treeManagerService= (TreeManagerService)context.getBean("treeManagerService");
Tree t=new Tree();
t.setName("1");
Tree t2=new Tree();
t2.setName("11");
Tree t3=new Tree();
t3.setName("111");
//将t2添加给t作为t的子集
t.getChildren().add(t2);
//添加父类地址t2.setParent(t);
t3.setParent(t2);
parent(t3);
children(t);
}}