Is it possible to find the present index in an enhanced for loop?
For getting the index, you can use List.indexOf(object).
I have used these techniques for getting index of current object in a list.
For example you given above, the removal can be done using two ways.
- Using object instance to remove object.
public boolean cancelTicket(Flight f, Customer c) {
List l = c.getBooking();
if (l.size() < 0) {
return false;
} else {
for (BookingDetails bd : l) {
if(bd.getFlight()==f){
l.remove(bd) // Object Instance here..
}
}
}
}
- Using List.indexOf(index)
public boolean cancelTicket(Flight f, Customer c) {
List l = c.getBooking();
if (l.size() < 0) {
return false;
} else {
for (BookingDetails bd : l) {
if(bd.getFlight()==f){
l.remove(l.indexOf(bd)) // index Number here..
}
}
}
}