Quiz: Is This Code Correct
Question text
- Sometimes it will not remove enough friends
- Sometimes it will remove too many friends
- Sometimes an IndexOutOfBoundsException will occur.
Sara doesn't tell you why the other answers can't work. Here goes:
The only time remove(i) is called is when friends.get(i).find(this) is -1, so friendly friends are never removed.
And i is always between 0 and friends.size() - 1. Therefore, the calls to get and remove never yield an IndexOutOfBoundException. Note that size() is called each time at the top of the loop, so it will report the shorter size whenever an element has been removed in the preceding iteration.