This method returns a list of employee objects using Cayenne SelectQuery
List<Employee> getEmployees() {
...
return getObjectContext().performQuery(query);
}
Now, I'm looping through the results
List<Employee> employees = getEmployees();
// test loop 1
for (Employee employee : employees) {
//print out employee name
}
for (Employee employee : employees) {
//print out employee name
if (matchesSomeCondition) {
employees.remove(employee);
}
}
// test loop 2
for (Employee employee : employees) {
//print out employee name
}
In test loop 1, it would show for example:
-John Smith
-Jane Doe
-Roger Wilco
In the actual removal loop, it would omit some people:
-John Smith
-Roger Wilco
Then in test loop 2 it would output everyone:
-John Smith
-Jane Doe
-Roger Wilco
When I change the removal loop to:
List<Employee> badEmployees = new ArrayList<Employee>();
for (Employee employee : employees) {
//print out employee name
if (matchesSomeCondition) {
badEmployees.add(employee);
}
}
employee.removeAll(badEmployees);
Then the loop works fine. I just don't understand why I did not get an error or exception in the first example. Even more bizarre is why the results are different in each of the test loop.