java中使用递归重复值获取字符串列表
我希望根据记录选择从表中列出名称 例如,如果我选择Id=1,那么我需要列表值(A、b、c、d)及其所有子对象 下面是我的表格数据 Id(1,2,3,4,5)姓名(a,b,c,d,e)家长Id(-,1,1,3,-) 下面是我的递归代码java中使用递归重复值获取字符串列表,java,recursion,Java,Recursion,我希望根据记录选择从表中列出名称 例如,如果我选择Id=1,那么我需要列表值(A、b、c、d)及其所有子对象 下面是我的表格数据 Id(1,2,3,4,5)姓名(a,b,c,d,e)家长Id(-,1,1,3,-) 下面是我的递归代码 public List<String> selectData(int orgId) { String parentId = String.valueOf(orgId); List<Object> listDa
public List<String> selectData(int orgId) {
String parentId = String.valueOf(orgId);
List<Object> listData = selectAllData();// here parent select
List<String> returnList = new ArrayList<String>();
for (Object parents : listData) {
List<String> list = this.getChildFor(parents.getId());
for (String child : list) {
returnList.add(child);
}
}
for(String enty : listData){
returnList.add(enty);
}
return returnList;
}
public List<String> getChildFor(int orgId) {
String parentId = String.valueOf(orgId);
int pId = 0;
List<Object> listData = selectByExample(); // here child select from all record based on orgId.
List<String> returnList = new ArrayList<String>();
do {
for (Object parents : listData) {
returnList.add(parents.name);
List<String> list = this.getChildFor(parents.getId);
for (String child : list) {
returnList.add(child.getname());
}
}
pId = 0;
} while (pId != 0);
for(String enty : listData){
returnList.add(enty);
}
listData = null;
return returnList;
}
public List selectData(intorgid){
String parentId=String.valueOf(orgId);
List listData=selectAllData();//此处为父选择
List returnList=new ArrayList();
对于(对象父对象:listData){
List List=this.getChildFor(parents.getId());
for(字符串子项:列表){
返回列表。添加(子项);
}
}
for(字符串条目:listData){
返回列表。添加(enty);
}
退货清单;
}
公共列表getChildFor(int orgId){
String parentId=String.valueOf(orgId);
int-pId=0;
List listData=selectByExample();//这里是根据orgId从所有记录中选择的子项。
List returnList=new ArrayList();
做{
对于(对象父对象:listData){
returnList.add(父母姓名);
List List=this.getChildFor(parents.getId);
for(字符串子项:列表){
returnList.add(child.getname());
}
}
pId=0;
}而(pId!=0);
for(字符串条目:listData){
返回列表。添加(enty);
}
listData=null;
退货清单;
}
在“returnList”列表中,我得到了重复的值。
任何人都可以告诉我这方面的想法,谢谢。在
getChildFor
方法中,您迭代了listData
两次
试着把它去掉
for(String enty : listData){
returnList.add(enty);
}
因为它已经添加到
returnList.add(parents.name)中代码>什么使您相信不应该得到重复的值?当我选择Id 1时,我得到的值类似于(a、b、a、b、c、d)。我需要的是id 1(a、b、c、d)的所有子对象的值。