package com; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class Test1 { List list2 = new ArrayList(); //构造数据库list public List<Student> getStudent(){ List<Student> list = new ArrayList<Student>(); list.add(new Student("2","1")); list.add(new Student("3","2")); list.add(new Student("4","3")); list.add(new Student("5","3")); return list; } //循环拿到所有的子类id public List getAll(String i,List<Student> list){ Iterator iter = list.iterator(); while(iter.hasNext()){ Student student = (Student)iter.next(); if(i.equals(student.getParentid())){ list2.add(student.getId()); getAll(student.getId(),list); } } return list2; } //测试 public static void main(String[] args) { List list = new Test1().getAll("1", new Test1().getStudent()); Iterator iter = list.iterator(); while(iter.hasNext()){ System.out.println((iter.next())); } } }