Cycle threw each "child"
in
Programming Questions
•
2 years ago
I got a structure of employees (similar to a file structure on a computer).
The "root" employees are stored in a arrayList (the employees with a depth of 0).
The class employee has a array to store employees aswell, for the deeper nested.
(code here is stripped to minium jus to show basic structure).
I can easilly print the total structure by calling this from main:
However, i want to draw a data circle of the employee tree.
For this i would like to go threw every employee just from the main tab.
But i have no idea how to cyle threw each employee from here,
something like this could work:
Or is the best way to have the draw function inside the Employee class itself?
- •
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
The "root" employees are stored in a arrayList (the employees with a depth of 0).
- class Location{
String name;
ArrayList<Employee> employees = new ArrayList<Employee>();
Location(String name){
this.name = name;
}
}
The class employee has a array to store employees aswell, for the deeper nested.
(code here is stripped to minium jus to show basic structure).
- class Employee{
Employee[] children = new Employee[0];
Employee(){
}; - void printList(){
printList(0);
};
// - - - - -
void printList(int depth){
// Print spaces for each level of depth
for (int i = 0; i < depth; i++){
print("\t");
}
//println(email);
//println(number);
println("•");
// Now handle the children, if any.
for(Employee employee : children){
employee.printList(depth + 1);
}
};
}
I can easilly print the total structure by calling this from main:
- for(Location location : locations){
println("\n"+location.name+"\n");
for(Employee employee : location.employees){
employee.printList();
}
}
However, i want to draw a data circle of the employee tree.
For this i would like to go threw every employee just from the main tab.
But i have no idea how to cyle threw each employee from here,
something like this could work:
- for(Location location : locations){
for(Employee employee : location.employees){
drawEmployee(employee);
for(Employee childEmp : employee.children){
drawEmployee(childEmp);
for(Employee childEmp : employee.children){
drawEmployee(childEmp);
for(Employee childEmp : employee.children){
drawEmployee(childEmp);
for(Employee childEmp : employee.children){
drawEmployee(childEmp);
}
}
}
}
}
}
Or is the best way to have the draw function inside the Employee class itself?
1