I have an arrayList with following values:
static ArrayList<DTONodeDetail> tree;
public static void main(String[] args) {
// TODO Auto-generated method stub
tree=new ArrayList<DTONodeDetail>();
//first argument->NodeId
//second->NodeName
// third -> ParentNodeId
tree.add(getDTO(1,"Root",0));
tree.add(getDTO(239,"Node-1",1));
tree.add(getDTO(242,"Node-2",239));
tree.add(getDTO(243,"Node-3",239));
tree.add(getDTO(244,"Node-4",242));
tree.add(getDTO(245,"Node-5",243));
displayTree(tree.get(0));
}
public static DTONodeDetail getDTO(int nodeId,String nodeName,int parentID)
{
DTONodeDetail dto=new DTONodeDetail();
dto.setNodeId(nodeId);
dto.setNodeDisplayName(nodeName);
dto.setParentID(parentID);
return dto;
}
Now i want to display above data in tree structure as below using simple java code:
Root
-----Node-1
------------Node-2
------------------Node-4
------------Node-3
------------------Node-5
I have tried following but unable to get desire result:
public static void displayTree(DTONodeDetail dto){
ArrayList<DTONodeDetail> childs = selectChild(dto.getNodeId());
System.out.println(dto.getNodeDisplayName());
for(DTONodeDetail obj:childs){
displayTree(obj);
}
}
public static ArrayList<DTOWorkSpaceNodeDetail> selectChild(int nodeID){
ArrayList<DTOWorkSpaceNodeDetail> list=new ArrayList<DTOWorkSpaceNodeDetail>();
for(int i=0;i<tree.size();i++)
{
if(tree.get(i).getParentID()==nodeID){
list.add(tree.get(i));
}
}
return list;
}
Please Provide some guide or code.
selectChild()? Also what is the output you get with this implementation?