SJSU/Udacity CS046

Lesson 7.1 - Array Lists and Arrays

  1. ArrayLists
  2. Elements
  3. Quiz: Drawing All the Elements
  4. Fixing the Overlap
  5. Quiz: Look at the Element Before this One
  6. Meet the Enhanced For Loop
  7. Quiz: Enhanced For Loop
  8. Quiz: Friends Done Right
  9. Quiz: Get Num Friends
  10. Refactoring
  11. Modifying an ArrayList
  12. Quiz: Modifying the Contents
  13. Quiz: Books To Read
  14. Quiz: Lost In A Good Book
  15. Quiz: Lost In A Good Book 2
  16. Quiz: Array Algorithms Sum Over All Elements
  17. Quiz: Array Algorithms Max and Min
  18. Quiz: Array Algorithms Count Matches
  19. Quiz: Array Algorithms Find the First Match
  20. Quiz: Array Algorithms Find All Matches
  21. Quiz: Separators
  22. Quiz: Unfriend Revisited
  23. Another Way to Implement Get Friends
  24. Quiz: Implementing Unfriend
  25. Quiz: Talk To
  26. Removing Mean Friends
  27. Quiz: Is This Code Correct
  28. Quiz: Implementing Removing Mean Friends

Quiz: Is This Code Correct

Question text

  1. Sometimes it will not remove enough friends
  2. Sometimes it will remove too many friends
  3. 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.

Problem on this page?

Your name:

Your email address:

Problem description:

To protect against spam robots, please answer this simple math problem:
× =