I have a class like this
public class GeographicalArea {
private String name;
private ArrayList
private ArrayList
. . . . . .
public GeographicalArea [] getAreayWithSubarea () {
/ / I want to achieve here return all child nodes
}
}
help. . Thank
------ Solution --------------------------------------- -----
public GeographicalArea[] getAreaWithSubarea() {
ArrayList<GeographicalArea> list = this.getSubarea();
GeographicalArea[] areas = new GeographicalArea[list.size()];
int i = 0;
for (GeographicalArea area : list) {
areas[i++] = area;
}
return areas;
}
private ArrayList<GeographicalArea> getSubarea() {
ArrayList<GeographicalArea> areas = new ArrayList<>();
for (GeographicalArea area : this.subAreas) {
areas.add(area);
areas.addAll(area.getSubarea());
}
return areas;
}
------ For reference only --------------------------- ------------
yesterday, you just ask the parent node of it. . Today asked the child node . .
You 'd think for themselves about , ah, such a basic issue of any book on this are basically able to speak it very clear
------ For reference only -------- -------------------------------
. . . . Thought out . . . .
I continue to think about . . . .
------ For reference only -------------------------------------- -
In fact, I have achieved depending on your method of recursive child nodes , but only print it, how can there not written into the array . . . .
sorry.
------ For reference only -------------------------------------- -
so I finally resolved . I have no way to put the results of written array , because this method is called recursively many times, so I re- wrote a class to recursion. Posted :
private List
public void recursionFn (GeographicalArea area) {
if (area.getSubAreas ()! = null && area.getSubAreas (). size ()> 0) {
areaArray.add (area);
Iterator
while (childIT.hasNext ()) {
GeographicalArea node = (GeographicalArea) childIT.next ();
recursionFn (node);
}
} else {
areaArray.add (area);
}
}
------ For reference only ---------------------------------- -----
private List<GeographicalArea> areaArray = new ArrayList<GeographicalArea>();
public void recursionFn(GeographicalArea area){
if(area.getSubAreas()!=null && area.getSubAreas().size() >0){
areaArray.add(area);
Iterator<GeographicalArea> childIT = area.getSubAreas().iterator();
while(childIT.hasNext()){
GeographicalArea node = (GeographicalArea)childIT.next();
recursionFn(node);
}
}else{
areaArray.add(area);
}
}
adjust the format of java, easy for everyone to see .
------ For reference only -------------------------------------- -
still thank you for helping .
knot a tie .
没有评论:
发表评论