47

Is it possible to find the present index in an enhanced for loop? If so how?

I am aware we can check it with an extra variable. But is there any other way.

public boolean cancelTicket(Flight f, Customer c) {
    List<BookingDetails> l = c.getBooking();
    if (l.size() < 0) {
        return false;
    } else {
        for (BookingDetails bd : l) {
            if(bd.getFlight()==f){
                l.remove()  // Index here..
            }
        }
    }
}
rgksugan
  • 3,521
  • 12
  • 45
  • 53
  • 6
    This doesn't make any sense: `if (list.size() < 0)`. Maybe you meant `==` instead? – Bruno Reis Oct 07 '11 at 11:25
  • 1
    Check this. http://stackoverflow.com/questions/1196586/calling-remove-in-foreach-loop-in-java – Vaandu Oct 07 '11 at 11:25
  • 3
    Even if you did have the index, attempting to remove the element from the list in an enhanced for loop would cause a ConcurrentModificationException. Use an iterator as suggested by @aioobe – John B Oct 07 '11 at 11:36

3 Answers3

43

Is it possible to find the present index in an enhanced for loop?

No. If you need the index, I suggest you use an ordinary for-loop.

However, you don't actually seem to need the index in this situation. Unless you're dealing with some really strange type of list, you could go with an Iterator and use the Iterator.remove() method, like this:

public boolean cancelTicket(Flight f, Customer c) {
    Iterator<BookingDetails> bdIter = c.getBooking().iterator();

    if (!bdIter.hasNext())
        return false;

    while (bdIter.hasNext())
        if (bdIter.next().getFlight() == f)
            bdIter.remove();

    return true;
}
Jesper
  • 202,709
  • 46
  • 318
  • 350
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • 7
    "You don't need the index for the remove operation in this particular situation though" - moreover, you *can't* use the index for the remove operation, because if you do, you will get a `ConcurrentModificationException`. As aioobe says, the correct way to do this is with `Iterator.remove()`. – Tom Anderson Oct 07 '11 at 11:35
18

Yes, Use a counter before the for statement. as such:

int i = 0;
for (int s : ss)
{
   // Some Code
   System.out.println(i);

   i++;
}
Izzy Kiefer
  • 189
  • 1
  • 4
10

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<BookingDetails> 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<BookingDetails> 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..
                }
            }
        }
    }
Ashish Nijai
  • 321
  • 2
  • 13