I got a problem with my function. That code works pretty fine for me, it removes an item when the boolean attribute is true:
private void doRemoveAusgefallenePlaene(Collection<CarPlan> pAllCarPlan) {
for (Iterator<CarPlan> lCarPlanIterator = pAllCarPlan.iterator(); lCarPlanIterator.hasNext();) {
CarPlan lCarPlan = lCarPlanIterator.next();
if (!lCarPlan.isAusfall()) {
lCarPlanIterator.remove();
}
}
}
My hope was to use the function for objects of type CarPlan
as well as ShipPlan
(both implements the interface Plan
).
private void doRemoveAusgefallenePlaene(Collection<? extends Plan> pAllPlan) {
for (Iterator<? extends Plan> lIterator = pAllPlan.iterator(); lPlanIterator.hasNext();) {
Plan lPlan = lPlanIterator.next();
if (!lPlan.isAusfall()) {
lPlanIterator.remove();
}
}
}
Now I can call the function with Collections
of Type CarPlan
and ShipPlan
doRemoveAusgefallenePlaene(pAllCarPlan);
doRemoveAusgefallenePlaene(pAllShipPlan);
and the code compiles without problems. Unfortunately at execution time a UnsupportedOperationException is thrown in line
lPlanIterator.remove();
I think I understand something wrong by declaring Collection<? extends Plan>
.
Knows anyone the problem?
-- UPDATE -- for those who are interested in one possible solution:
final List<CarPlan> lAllCarPlan = new ArrayList<CarPlan>(pAllCarPlan);
doRemoveAusgefallenePlaene(lAllCarPlan);
works great with ShipPlan
and CarPlan
.