I have a strange problem to which I know the work around but I want to do it with array list this time. Here is the problem: I have a tree of employees. Employee is a simple class (under is list of employees that work for this employee) :
class Employee
{
String name;
ArrayList<Employee> under = new ArrayList<Employee>();
//fire function
}
My task is to recursively fire all the employees which do not have employees under them. I know how to this with work around with custom made list data structure but I want to do it with array list. Here is my code so far:
public boolean Fire()
{
if (under.isEmpty())
return true;
else
{
for (int x = 0; x < under.size(); x ++)
{
if (under.get(x).Fire())
under.remove(x);
}
}
return false;
}
But problem for this code is that when I remove under.remove(x) the under.size() gets smaller and indexes get messed up. I tried to set x = 0, after every under.remove(x) but it did not do exactly right. One employee to much still left. Any solutions with array list structure?